What are the uses of using in C#
What are the uses of using in C#
Here is an explanation of the uses of the using
statement in C#:
The using
statement in C#:
IDisposable
interface.Dispose
method is called on the object when the associated using
block is exited.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.
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.
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
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào