struct (struct) in C # is a value type, struct type full contact

Source: Internet
Author: User

As is known to all, struct types are very similar to class types, especially in C + +, where class can do almost anything a struct can achieve. The Struc type still exists in C #, and its usefulness is relatively less important, and here is a big difference between struct and class in C #, where a struct is a value type and class is a reference type.

Cases:

Class Program

{

struct Test

{

public int A;

}

static void Main (string[] args)

{

Test test1 = new test ();

test1.a = 1;

Test test2 = test1;

Console.WriteLine ("Test1.a=" + test1.a);

Console.WriteLine ("Test2.a=" + test2.a);

test2.a = 2;

Console.WriteLine ("Test1.a=" + test1.a);

Console.WriteLine ("Test2.a=" + test2.a);

Console.ReadLine ();

}

}

To execute the above program, the output will be:

Test1.a=1

Test2.a=1

Test1.a=1

test2.a=2

Test2 changes have no effect on test1, test1 and test2 correspond to two different memory areas, and if the struct is changed to class, it will be output:

Test1.a=1

Test2.a=1

test1.a=2

test2.a=2

C # Specifies the basic type as a value type, and the larger type that contains many fields as a reference type, the biggest reason for C # Design This way is to get maximum performance. When we are developing a project, the custom type can choose either class or struct, but if you want your type to be a value type (sometimes it is necessary), you should declare it as a struct type.

In addition, there are many features of the structure that need our attention and are summarized as follows:

1. All structs derive from the object class and cannot be derived from other classes or structs or as base classes of other classes or structs.

2. The fields in the structure are private by default, and the fields of the structure cannot be initialized at the time of declaration.

3. Structs can have one or more construction methods, but do not allow themselves to define default constructors, and the compiler requires custom constructors to initialize all fields of the structure.

4. The default constructor of the struct sets the field of all value types to 0, and all reference types are set to a null reference.

The following is a description of 1, the struct type can not be derived from other classes or structs is that we can not display the addition of the inherited statement ": SomeClass", in fact, when we define the structure type, the system is implicitly inherited, the inherited base class is System.ValueType. Take a look at the following example:

struct Test
{
public int A;
public int b;
}

A struct type test is defined above, and the following is its IL code:

. class private sequential ANSI sealed beforefieldinit Consoleapplication1.test
extends [Mscorlib]system.valuetype
{
}//End of Class Consoleapplication1.test
We can see very clearly that test inherits the System.ValueType class. Reader friends can check it out. NET all the basic data types, they are all struct types (defined with the struct keyword), and implicitly inherit the System.ValueType during the defined process.

struct (struct) in C # is a value type, struct type full contact

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.