Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call C # instantiation and static class objects

Shulou Source: shulou.com Published: 2022-05-31 20:38:20 09月13日 Update

This article mainly introduces the relevant knowledge of C# instantiation and how to call static class objects, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to instantiate C# and static class objects. Let's take a look.

1. Contrast effect

No matter how many times the instantiated object is called, it will re-initialize the value call, indicating that the life cycle of the instantiated object ends after the call is completed.

Static class object, the static members under the static class will always occupy memory and only instantiate once, indicating that the life cycle of static class members is from the call to the end of the program or mechanism recycling.

The scene uses:

1) static class or member: database connection string, which can be defined as static, which is called and obtained frequently.

2) instantiation: login operation. General login operation is not too frequent.

Restart the corresponding IIS site

When constantly refreshing a page, the memory increases regularly according to the 0.1MB. After refreshing to a certain number of times, the memory does not increase significantly, or even remains the same. There are two .NET Core Host processes in the following figure, which refer to two sites and are called externally or internally, otherwise the status will not be displayed.

2. Instantiation code public string init = "instantiation: hello"; public bool flag = false;public int num = 0 flag public void Test (string one) {string test = ""; if (! flag) {flag = true; num + = 1; test = "[first output]" + init + one + num;} else {num + = 1; test = init + one + num } LmyLogHelper.Debug (test);} 3. Static class code public static string init = "static: hello"; public static bool flag = false;public static int num = 0polipublic static void Test (string one) {string test = ""; if (! flag) {flag = true; num + = 1; test = "[first output]" + init + one + num;} else {num + = 1 Test = init + one + num;} LmyLogHelper.Debug (test);} this article on "how to instantiate C# and how to call static class objects" ends here, thank you for reading! I believe you all have a certain understanding of the knowledge of "how to instantiate C# and how to call static class objects". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Static instance object CVM memory member knowledge frequent two code content cycle life site article login output obvious following figure constantly Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Microsoft Shulou Tech Info Linux OPPO Reno