New line types

From Pro C #

Recalling the "New" Internal Data Types ...

All built-in data types support the so-called default constructor. It allows you to create a variable using a new keyword.

[...] References to objects (including strings) are null.

In C #, strings do not have a public default constructor. I assume that due to the immutability of the string, they have their own default constructor. But here we are talking about references to objects and in general in the string when using new .

Because you can’t do

String myString = new String();

So,

String a;

a link to a string does not result in a "default". Instead, it is a compiler error to access.

Although

 public class StringContainer { public static string myString { get; set; } } 

Results in a legally accessible string (null by default). It does not use new . He performs some kind of magical construction.

What happens in StringContainer scenerio? Since there is no standard default constructor in the line, is this a mistake in the C # book?

+4
source share
3 answers

All built-in data types support the so-called default constructor. It allows you to create a variable using a new keyword.

There is an impressive amount of subtle errors in this expression.

Firstly, there is no such thing as an “internal” data type; perhaps this term is defined elsewhere in the book?

Secondly, it would be more accurate to say that all types of structures have an open constructor without parameters, called the default constructor. Some types of classes also have ctor open without parameters; if you do not provide any ctor, then the C # compiler automatically generates a public ctor without parameters for you. If you provide ctor, then the C # compiler will not do this for you.

Third, constructors do not create variables . The author combines many related, but different things: the "new" operator, memory manager, constructor and variable, and the created object. Variables are storage locations and are managed by the CLR; they are not created by the "new" operator.

The correct statement is that the “new” statement in the structure causes the CLR variable to be created in the temporary storage pool; this variable is then initialized by the memory manager, and then passed to the constructor for greater initialization. The value created in this way is then copied somewhere else. The "new" statement in the class forces the CLR to create an object in the long-term storage pool, and then pass the reference to this object to the CLR. No "variable" required.

Knocking variables with objects is a very common mistake; it’s helpful to understand the difference.

In C #, strings do not have a public default constructor.

Right.

I assume that due to the immutability of the strings, they have their own default constructor.

Good, but wrong. There is no built-in constructor without parameters in the line.

[auto-property or field of type string] leads to a legitimate accessible string (default is null). It does not use new ones. He performs some kind of magical construction.

This is not true. A null reference is not a constructed object at all. This is the lack of a built object!

You basically say that my empty garage contains a “magically constructed” non-existent car. This is an extremely strange way to look at an empty garage; an empty garage does not contain a car, not a magically constructed non-existent car.

What happens in a StringContainer script?

The containing type contains the field generated by the compiler - a variable - type strings. Suppose the contained type is a structure or class. When storage for a structure or class is initialized by the memory manager, the memory manager writes a null reference to the storage location associated with the variable.

Finally: I suspect that your confusion is due to the fact that you got the default confusion of "default constructor" and "default value for type". For the structure, they are one and the same:

 int x = new int(); 

and

 int x = default(int); 

both make int initialization zero.

For a class, they do not do the same:

 Fruit f = new Fruit(); 

makes a new reference to fruits and assigns a reference to the variable f, whereas:

 Fruit f = default(Fruit); 

coincides with

 Fruit f = null; 

The constructor is not called.

+17
source

All built-in data types support the so-called default constructor. It allows you to create a variable using a new keyword.

I am not sure what the author means by "internal data types". It is best to assume that it actually means “value types” (that is, Types declared with the C # struct ), because value types always have a default constructor, and reference types may not exist.

So, if you have a field whose type is a struct type (for example, Int32, CancellationToken), then this field will be initialized as if the default constructor of the type were called.

In a real implementation, there is probably no actual call to the default constructor by default - the memory is only initialized to all zeros, which is the same as when the default constructor is called. (That's why you cannot provide your own constructor without parameters for a value type - a constructor without parameters always initializes memory for all zeros. This greatly simplifies things like new int[10000] - the compiler should not actually call new Int32() 10 000 times, but just turns off the memory.)

Your question about the string field in a class is not really related to the author’s discussion of “internal data types”, since both string and your class are reference types, not value types. Thus, your class will not have without parameters- constructor-this-you-can't-override; it will only have regular constructors. But the nulling behavior still exists: when you call the constructor, the new memory block is nullified before the constructor code is run. The string field is a reference type, and the null reference is null .

+5
source

I assume that it uses default (string), which returns null, because the string is a reference type.

Also, remember that constructors cannot return null.

+2
source

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


All Articles