关键字“using”用于定义该 using 语句块中使用的资源的范围。一旦代码块完成执行,在 using 代码块中使用的所有资源都会被释放。请参考以下示例。
class Books : IDisposable { private string _name { get; set; } private decimal _price { get; set; } public Books(string name, decimal price) { _name = name; _price = price; } public void Print() { Console.WriteLine("Book name is {0} and price is {1}", _name, _price); } public void Dispose() { throw new NotImplementedException(); } } class Students { public void DoSomething() { using (Books myBook = new Books("book name", 12.45)) { myBook.Print(); } } }
关键字“using”用于定义该 using 语句块中使用的资源的范围。一旦代码块完成执行,在 using 代码块中使用的所有资源都会被释放。请参考以下示例。