Câu hỏi phỏng vấn C#
Câu hỏi

What are the uses of using in C#

Câu trả lời

Here is an explanation of the uses of the using statement in C#:

Using Statement

The using statement in C#:

  1. Provides a convenient syntax to work with objects that implement the IDisposable interface.
  2. Ensures that the Dispose method is called on the object when the associated using block is exited.
  3. Simplifies resource management by automatically handling the disposal of objects.

The using statement is commonly used with objects that require cleanup, such as file streams, database connections, or any other resources that implement the IDisposable interface.

Using Syntax

The syntax for the using statement is as follows:

using (expression) 
{
    // Code block
}

The expression is an object that implements the IDisposable interface. It is typically a constructor call that returns an object.

Example

Here's an example of using the using statement with a StreamReader object:

string filePath = "example.txt";

using (StreamReader reader = new StreamReader(filePath))
{
    string content = reader.ReadToEnd();
    Console.WriteLine(content);
}

In this example:

1....

middle

middle

Gợi ý câu hỏi phỏng vấn

expert

What is the use of static constructors?

senior

Can you explain the difference between Interface , abstract class, sealed class, static class and
partial class in C#?

middle

What is the use of the IDisposable interface?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào