Unity3D and C# study notes record-the usage of Using

                                                    Unity3D and C# study notes record-the usage of Using


table of Contents

1. Blog introduction

2. Content

(1) Function

(2) Simple example

(3) Multiple citation examples

3. Push

4. Conclusion


1. Blog introduction

       I have been busy recently, and I haven’t updated the blog for a long time. I will slowly respond to the update rate after the year. I will gradually improve the Unity framework content after the year. Then I will update some plug-in learning blogs from time to time. I hope in the new year. Learn more, and hope that everyone who reads the article can also get their own satisfactory results. This blog is used as a knowledge point learning record. Regarding the usage of Using, we generally refer to dll or module. We have read it in the last two days. A bunch of codes are used as follows. I have some doubts. I checked it a little and made this record.

                using(ES2Reader reader = new ES2Reader(settings))
		{
			while(reader.Next())
				headers[reader.currentTag.tag] = reader.ReadHeader();
		}

2. Content

(1) Function

       Let’s briefly introduce the function. Let’s instantiate some classes in the parentheses after the Using keyword, and then directly call the instanced classes in the method body below. These classes cannot be called outside of the Using method, and they are in the body of the Using method. After the execution is completed, the class will be automatically released. Note that the class of this instance must inherit the IDisposable interface, otherwise it cannot be compiled, and the Dispose method overridden by the class will be automatically called when released.

(2) Simple example

First create a class that inherits from the IDisposable interface and add an open function

public class Test1 : IDisposable {

	public void PrintData(string str)
	{
		Debug.Log("--------------:"+str);
	}

	public void Dispose()
	{
		Debug.Log("释放Test1");
	}
}

Then use in Using

public class TestSave : MonoBehaviour {
	void Start () {
		using (Test1 test1 = new Test1()) 
		{
			test1.PrintData("This is a test1");
		}
		print("Using语句结束");
	}
}

The following is the output

(3) Multiple citation examples

Using can not only refer to a single example, but also multiple examples at the same time. The usage is as follows:

Add another class that inherits from the IDisposable interface and add an open function

public class Test2 : IDisposable {

	public void PrintData(string str)
	{
		Debug.Log("--------------:"+str);
	}
	public void Dispose()
	{
		Debug.Log("释放Test2");
	}
}

Then use in Using

public class TestSave : MonoBehaviour {
	void Start () {
		using (Test1 test1 = new Test1()) using(Test2 test2 = new Test2())  
		{
			test1.PrintData("This is a test1");
			test2.PrintData("This is a test2");
		}
		print("Using语句结束");
	}
}

The input results are as follows:

Note : One thing to note here is that the order of release is opposite to the order of references, and the first referenced instance will be released at the end


3. Push

Github:https://github.com/KingSun5


4. Conclusion


        If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/103932121