C # l will the compiler generate the default constructor for the value type!

Source: Internet
Author: User

C # Does the compiler generate a default constructor for the value type?

The answer is no. the C # compiler does not generate Default constructors for values. This design is based on performance considerations, another reason is that the value type can be implicitly created! This can be verified through the ildasm tool. The value type does not generate the default constructor. See the followingCode:

Class Program
{
Static void main (string [] ARGs)
{
A A = new ();
}
}
Struct
{

}

Someone may be surprised: A = new A (); this line calls the default constructor of the struct. If the default constructor is not generated, it should not be compiled here, as expected, this line of code can be compiled!

There seems to be contradictions here! Don't worry. Let's take a look at the main method's il code:

. Method private hidebysig static void main (string [] ARGs) cel managed
{
. Entrypoint
// Code size 10 (0xa)
. Maxstack 1
. Locals Init ([0] valuetype consoleapplication1.a)
Il_0000: NOP
Il_0001: ldloca. s
Il_0003: initobj leleapplication1.a
Il_0009: Ret

} // End of method program: Main

Let's look at the red line of code. The first line is to import the local variable A into the stack, and the second line is to initialize a. Note that the initobj command is used, here we call the implicit default constructor, because each value type has an implicit default constructor to initialize the default value of this type!

If it is changed to the reference type, the newobj command should be used. Then, the default constructor generated by the C # compiler is called!

In fact, this is why the member declaration in the value type cannot be directly initialized, because the C # compiler does not generate a default constructor for the value type at all, so Where can it be initialized!

Hope this articleArticleIt can help you a little bit!

Thank you for your participation!

I will reply to you here:

The first floor is correct. in C #, struct cannot define constructors without parameters. Only constructors with parameters can be defined. This design is important to avoidProgramMisunderstanding!

For example:

Class myclass

{

Public A; // some people may mistakenly think that the default constructor of A will be automatically called when constructing a myclass instance. The idea is generally found in C ++ experience, here C # is different from C ++

}

To avoid such confusion, the default no-argument constructor is not allowed in the struct in C!

The usage of struct may be misunderstood! In fact, the struct in C # is very useful, not just to be compatible with legacy components.

The following is a detailed description:

As we all know, struct is a value type, so his instance is stored in the stack, and the access speed of the stack is faster than that of the managed stack, but the space is not huge!

Therefore, we can define some small types as structures to speed up access, for example:

Struct point

{

Int X;

Int y;

}

We may wonder why not all types are defined as value types since the access speed is fast? The reason is relatively simple. The stack space is not very large and it cannot store a lot of things. In addition, even instances with the same value in the stack usually need to store two copies, for example:

Point P1 = new point ();

Point P2 = p1;

There are two P1 copies in the stack!

In this way, space is occupied!

Therefore, only some small types are suitable for definition as struct. It is better to define a large type as a reference type and put it in a hosted heap with a relatively large space!

Thank you again for participating in the discussion! Thank you!

 

Related Article

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.