简体   繁体   English

C#代码在做什么:

[英]What is C# code doing:

In the following code: 在下面的代码中:

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

What is the use of first constructor? first构造函数的用途是什么?

The first constructor is passing null into parameter b of the second constructor. 第一个构造函数将null传递给第二个构造函数的参数b。

Thus if you call new A() it will be the same as calling new A(null) 因此,如果您调用new A() ,它将与调用new A(null)

When 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 "" as a parameter. 默认构造函数实际上以“”作为参数调用“参数构造函数”。 You are passing a parameter. 您正在传递参数。 This is done in order to avoid writing the same code twice 这样做是为了避免两次编写相同的代码

It's a constructor overload . 这是一个构造函数重载

I agree it doesn't seem to be very useful in this case because most likely the uninitialised value for a string is null anyway. 我同意在这种情况下它似乎不是很有用,因为字符串的未初始化值很可能还是为null。

See also Constructors in C# 另请参见C#中的构造方法

this happens when you're overloading constructors. 当您重载构造函数时,会发生这种情况。

in your example the empty contructor public A():this(null){} looks for a constructor that can take in an object value of null. 在您的示例中,空的构造函数public A():this(null){}查找可以接受对象值为null的构造函数。 since a string is an object that can take nulls, it calls that constructor. 由于字符串是一个可以为null的对象,因此它将调用该构造函数。

this example seems very simplistic. 这个例子似乎很简单。

a more meaningful example (but still keeping it basic): 一个更有意义的示例(但仍保持基本状态):

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

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

in this example, when a calling program calls the empty constructor, it will output 200. because it is calling the AddNumbers method with x = 100, y = 100. 在此示例中,当调用程序调用空构造函数时,它将输出200。因为它正在使用x = 100,y = 100调用AddNumbers方法。

i know it's a simple example but i hope that makes it clearer. 我知道这是一个简单的示例,但我希望这可以使它更清楚。

这是一个默认构造函数,使用b == null调用秒。

Some interfaces or designers require there to be a "parameterless" constructor. 一些接口或设计器要求那里有一个“无参数”构造函数。

This method comes in handy in those times. 在那个时候,这种方法很方便。

Having a parameterless default constructor is required when object initialization is used: 使用对象初始化时,需要具有无参数的默认构造函数:

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

In this case, I probably would not use constructor chaining, though. 在这种情况下,我可能不会使用构造函数链接。 Constructor overloading gives you an alternative way to initialize with parameters. 构造函数重载为您提供了一种使用参数进行初始化的替代方法。 Where constructor chaining is really useful is in making constructor parameters optional; 构造器链真正有用的地方是使构造器参数可选。 C# doesn't support optional parameters (yet). C#目前尚不支持可选参数。

"Best practice" will depend on the situation, usage, architecture, requirements, etc. (ISO Consulting Rule Number One: "It depends.") “最佳实践”将取决于情况,用法,体系结构,要求等(ISO咨询规则第一:“取决于”。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM