Description of C # static Constructor
Static constructor is a new feature of C #, which is rarely used. However, we need to use static variables when initializing them. This constructor belongs to a class rather than an instance. That is to say,
Static constructor, java static Constructor
Let's take a look at a common question. What is the execution result of the following code?
class A { public static int X = B.Y + 1; static void Main(string[] args) {
How can we correctly understand C ++ static constructor? What role does this static function play in practical applications? You will find these answers in this article. I hope this content will help you better understand C ++.
Summary of C ++
First, let's look at a conclusion table.
If (with class explicit static constructor){If (with access to static members){After entering the main function, call the static constructor before accessing the static member for the first time, and no
Beforefieldinit and class static Constructor
Luo chaohui (http://kesalin.cnblogs.com /)
This article follows the "signature-non-commercial use-consistency" creation public agreement
As follows:Code:
Using System; Namespace Beforefieldinit
When writing a program, it is inevitable to use static members. Because the access to static members is out of type objects, it is unreasonable to use non-static constructors, private methods, or some other methods .. NET provides the Member
In C #, the static constructor of a class is used for initialization before the class is used. For example, it initializes static members or performs specific operations. The CLR automatically calls the static constructor when it first creates the
Objective C # Item 13: initializing static members using static ConstructorsWriteProgramIt is inevitable to use static members. Because the access to static members is out of type objects, it is unreasonable to use non-static constructors, private
static variables
The static variable is on the stack and is a global variable that has been generated at compile time.
public class Cow
{public
static int count;
private int id;
Public Cow ()
{
id = ++count
}
}
The client creates 2 cow
Visual Studio 2010In other versions, this topic has not been rated.
A static constructor is used to initialize any static data or perform a specific operation that only needs to be performed once. The static constructor is automatically called
A static constructor is used to initialize any static data or perform a specific operation that only needs to be performed once. The static constructor is automatically called before the first instance is created or any static member is referenced.
Static constructor======================================First, let's look at a piece of code:
Namespace Learning. Static{Class StaticTesting{Static void Main (string [] args){Test t1 = new test ();Test t2 = new test ();Console. WriteLine (test.
(1) It is used to initialize static and read-only fields. (2) You cannot add access modifiers when adding static keywords because static constructors are private. (3) Class static constructor in a given application Program Execute at most once in a
Think about the output results of the three-segment program below the limit:Using System;
Public class Type1
...{
Public static int I;
Static Type1 () // explicitly defines the static Constructor
...{
Console. WriteLine ("Explicit: In Type1 Class
The static constructor can be used in C # to initialize class data in the same way as the instance constructor used to initialize instance data. There are some differences between static constructor and instance constructor rules. Unlike the
• The static constructor neither has an access modifier nor a parameter.• If a static constructor is not compiled and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.• When
• The static constructor neither has an access modifier nor a parameter.
• If a static constructor is not compiled and the class contains a static field with an initial value, the compiler automatically generates the default static constructor.
•
Static constructor is a new feature of C #, which is rarely used. However, we need to use static variables when initializing them. This constructor belongs to a class rather than an instance. That is to say, this Constructor
Executed only once .
Http://www.cnblogs.com/michaelxu/archive/2007/03/29/693401.html
Static constructor is a new feature of C #, which is rarely used. However, we need to use static variables when initializing them. This constructor belongs to a class rather than
Initializer in the above to "initialize", it is not good to hear, this article is translated into: "Preset method")
You should know that before any instance of a type initializes, you should initialize its static member variable. In C # You can use
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.