The difference between struct and class in C # and their applicability

Source: Internet
Author: User

First difference, the source of the original http://www.dotnetspider.com/resources/740-Difference-between-class-struct-C.aspx
1. Class is the reference type, and structs is the value type.
Since the class is a reference type, the class can be set to null. However, we cannot set struct to null because it is a value type.

 struct AStruct
{
int aField;
}
class AClass
{
int aField;
}
class MainClass
{
public static void Main()
{
AClass b = null; // No error.
AStruct s = null; // Error [ Cannot convert null to 'AStruct' because it is a value type ].
}
}

2. When you instance a class, it will be created on the stack. And your instance has a struct, which will be created on the stack.

3. You are using a reference to the class instance. Instead of referencing a struct. (Instead, use them directly)

4. When we pass the class as a parameter to a method, we pass a reference. Struct passes a value rather than a reference.

5. structs cannot have an initializer, and class can have an initializer.

 class MyClass
{
int myVar =10; // no syntax error. public void MyFun( )
{ // statements }
}
struct MyStruct
{
int myVar = 10; // syntax error.
public void MyFun( )
{ // statements }
}

 

 

6 class can have a non-parameter constructor, but struct cannot.

 class MyClass
{
int myVar = 10;
public MyClass( ) // no syntax error.
{
// statements
}
}
struct MyStruct
{
int myVar;
public MyStruct( ) // syntax error.
{
// statements
}
}

 

 

Class 7 must be instantiated with the New Keyword before use, and struct does not need

 MyClass aClassObj; // MyClass aClassObj=new MyClass(); is the correct format.aClassObj.
myVar=100;//NullReferenceException(because aClassObj does not contain a reference to an object of type myClass).
MyStruct aStructObj;
aStructObj.myVar=100; // no exception.

 

 

8 class supports inheritance and polymorphism, but struct does not. Note: however, struct can implement the same interface as the crane class.
9 since struct does not support inheritance, its members cannot be modified with protected or protected internal
10 class constructor does not need to initialize all fields, and struct constructor must Initialize all fields

 class MyClass //No error( No matter whether the Field ' MyClass.myString ' is initialized or not ). 
{
int myInt;
string myString;
public MyClass( int aInt )
{ myInt = aInt; }
}
struct MyStruct // Error ( Field ' MyStruct.myString ' must be fully assigned before it leaves the constructor ).
{
int myInt;
string myString;
public MyStruct( int aInt )
{
myInt = aInt;
}
}

 

 

11 class can define the destructor, but struct cannot.
12 class is suitable for big and complex data, and struct is suitable for new types of data that are frequently used in combination.

Applicable scenarios: struct has a performance advantage and class has an object-oriented extension advantage.
The Type Used for underlying data storage is designed as the struct type, and the type used to define application behavior is designed as a class. If you cannot determine the future application of the type, you should use class.

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.