The difference between struct and class in C #

Source: Internet
Author: User

The most essential difference between class and struct is that class is a reference type, whereas a struct is a value type, which differs in memory allocation.

What is class?

Class is the basic concept of object-oriented programming, and is a custom data structure type that typically contains fields, properties, methods, properties, constructors, indexers, operators, and so on. In. NET, all classes end up inheriting from the System.Object class, so it is a reference type, that is, when an instance of new class is placed on the stack (stacks), the address of that instance in the managed heap (managed heap) is stored. The value of the instance is saved in the managed heap (managed heap).

What is a struct?

A struct (struct) is a value type that organizes a set of related variables into a single variable entity. All structures inherit from the System.ValueType class and are therefore a value type, that is, the struct instance is allocated on the thread's stack (stack) when it is created, and it stores the value itself. So when using a struct, we can treat it as a basic type class such as int and char.

1,class is a reference type, structs is a value type

Since class is a reference type, class can be set to null. However, we cannot set the struct to null because it is a value type.

Namespace Ax

{

public partial class Form3:form

{

Public Form3 ()

{

InitializeComponent ();

Structa x = null; Error: Unable to convert null to ' structa ' because it is a type that is not a null value

ClassA y = null;

}

}

}

public struct STRUCTA

{

public int A;

}

public class ClassA

{

public int A;

}

2, when you instantiate a class, it will be created on the heap. While you instantiate a struct, it will be created on the stack

3, you are using a reference to the class instance. And you're not using a reference to a struct. (instead of using them directly)

4, when we pass class as a parameter to a method, we are passing a reference. A struct passes a value rather than a reference.

5,structs can not have initializers, class can have initializers.

public struct STRUCTA

{

public int A = 90; Error: "STRUCTA.A": cannot have instance field initial value in struct

public int A;

}

public class ClassA

{

public int A = 90;

}

6,classes can have an obvious parameterless constructor, but the struct can not

public struct STRUCTA

{

public int A = 90; Error: "STRUCTA.A": cannot have instance field initial value in struct

public int A;

Public structa ()///error: struct cannot contain an explicit parameterless constructor

//{

This. A = 0;

//}

Public structa (int paraa)//ok

{

This. A = Paraa;

}

}

public class ClassA

{

public int A = 90;

Public ClassA ()

{

This. A = 90;

}

}

7, the class must be instantiated with the new keyword before it is used, and the struct does not need

Public Form3 ()

{

InitializeComponent ();

Structa x = null; Error: Unable to convert null to ' structa ' because it is a type that is not a null value

Structa x;

X.A = 8;

Structa x2 = new Structa ();

X2. A = 32;

ClassA y = null;

ClassA y1 = new ClassA ();

Y1. A = 4;

ClassA Y2;

Y2. A = 5;//Error: An unassigned local variable "y2" is used

}

8,class supports inheritance and polymorphism, and struct does not support. Note: But structs can implement interfaces like classes

9, since the struct does not support inheritance, its members cannot be decorated with protected or protected Internal

The 10,class constructor does not need to initialize all fields, and the constructor of the struct must initialize all fields

public struct STRUCTA

{

public int A;

public int B;

Public structa (int paraa, int parab)//ok

{

This. A = Paraa;

This. B = Parab;

}

Public structa (int paraa)//ok

//{

This. A = Paraa;

This. B = Paraa;

//}

Public structa (int paraa, int parab)//Error: Field "STRUCTA.B" must be fully assigned before control returns to caller

//{

This. A = Paraa;

//}

}

public class ClassA

{

public int A = 90;

public int B;

Public ClassA ()

{

This. A = 90;

}

}

11,class can define the destructor, but the struct can not

12,class is more suitable for large and complex data, and structs are suitable for new types that are combined as some of the data that is often used.

Application: struct has a performance advantage, class has an object-oriented expansion advantage.

The type used for the underlying data store is designed as a struct type, and the type used to define the application behavior is designed as class. You should use class if you are unsure of the future application of the type.

The difference between struct and class in C #

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.