C # advanced (3) constructor

Source: Internet
Author: User

A constructor is a method of the same class name. If no explicit declaration is made, the system will automatically generate a class without parameters during compilation, constructor that does not execute any action.

However, if the constructor is explicitly declared, the system will not automatically generate the constructor. If the declared constructor is a constructor with parameters, we must instantiate the class using this constructor when instantiating the class. See the followingCode:

Code
Using System;
Namespace Gosoa.com.cn
{
Public Class Test
{
Public Int Num;
Public Test ( Int I)
{
This . Num = I + 5 ;
}

Static VoidMain ()
{
Test classone=NewTest (10);
IntX=Classone. Num;
Console. writeline (X );
}
}
}

 
 

 

The code above indicates that when instantiating a class,Test classone = new test (10 );A parameter is passed. If weTest classone = new test ();In this way, the class will be instantiated and an error will be reported. Because we explicitly declare a constructor with parameters,New Test ()In this way, the constructor without parameters is called, but there is no constructor without parameters in the class.

 

Let's take a look at the static constructor.

InC #We can define a static constructor without parameters for the class (Note that there must be no parameters). As long as the class object is created, this method will be executed. This function is executed only once and before the code reference class.

Generally, there are some static fields or attributes in the class. You need to initialize these static fields and attributes from the external data source before using the class for the first time. In this case, we will use the static constructor method to solve this problem.

The static constructor does not have an access modifier.C #The Code does not call it either. When the class is loaded. NetThe Runtime Library calls it. A class can have only one static constructor.

Note that the instance constructor without parameters can coexist with the static constructor in the class. The static constructor is executed when the class is loaded, and the instance constructor is executed when the instance is created. The two are not in conflict.

Let's look at the example below.

 

Code
Using System;
Namespace Gosoa.com.cn
{
Public Class Test
{
Static Test ()
{
Console. writeline ( " Www.gosoa.com.cn " );
}
Public Test ()
{
}

Static VoidMain ()
{
Test classone=NewTest ();
}
}
}

 
 

 

TheProgramThe result is Www.gosoa.com.cn When a class object is created, the static constructor is already running.

Let's look at an example.

 

Code
Using System;
Namespace Gosoa.com.cn
{
Public Class Test
{
Private String Domain;
Private String URL;
Public Test ( String Dom, String URL)
{
This . Domain = Dom;
This . Url = URL;
}
Public Test ( String Dom)
{
This . Domain = Dom;
This . Url = " Gosoa.com.cn " ;
}

Static VoidMain ()
{
Test classone=NewTest ("Gosoa");
Console. writeline (classone. url );
}
}
}

 
 

 

In the preceding example, there are two constructors. It is possible that the two constructors need to initialize the same field. In this case,C #There is a special language called the constructor. See the following code:

 

 

Code
Using System;
Namespace Gosoa.com.cn
{
Public Class Test
{
Private String Domain;
Private String URL;
Public Test ( String Dom, String URL)
{
This . Domain = Dom;
This . Url = URL;
}
Public Test ( String Dom ): This (DOM, " Www.gosoa.com.cn " )
{
}

Static VoidMain ()
{
Test classone=NewTest ("Gosoa");
Console. writeline (classone. url );
}
}
}

 
 

 

 

The above instance is used Constructor. Note that the constructor is executed before the constructor.

 

2 , read-only field (readonly) .

Read-Only fields are more flexible than constants, constant (const) fields must be initialized at the beginning of the declaration, but readonly fields can even be computed to determine their values.

Note: You can assign values to read-only fields in the constructor, but not elsewhere.

 

 

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.