What C # code does:

In the following code:

public class A
{
public A():this(null){}
public A(string b){/*code here*/}
}

What is the use of the first constructor?

+3
source share
7 answers

The first constructor passes nullto parameter b of the second constructor.

That way, if you call new A(), it will be the same as the callnew A(null)

+26
source

If you have a constructor with a parameter

public A(string b){ /* code here */ }

public A():this("") { }  //default

the default constructor actually calls the "parameter constructor" with the "" parameter. You pass the parameter. This is done so as not to write the same code twice

+5
source

.

, , , , .

. #

+3

, .

public A():this(null){} , null. - , , .

.

( ):

 public class AddNumbers
{
   public AddNumbers():this(100, 100)
   {     }

   public AddNumbers(int x, int y)
   {     
         int sum = x + y;
         Console.WriteLine(sum.ToString());
   }    
}

, , 200. AddNumbers x = 100, y = 100.

, , , .

+3

, b == null.

+1

.

.

0

:

Employee e = new Employee() {FirstName="John", LastName="Smith"};

, , . . , , ; # ().

" " , , , .. (ISO Consulting Rule Number One: " ".)

0

Source: https://habr.com/ru/post/1721472/


All Articles