This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22710|Reply: 0

[Source] The difference between struct and class in C#

[Copy link]
Posted on 6/24/2016 11:53:06 AM | | |
The most essential difference between class and struct is that class is a reference type, while struct is a value type, and their allocation in memory is different.

What is class?

class, a fundamental concept in object-oriented programming, is a type of custom data structure that typically contains fields, attributes, methods, attributes, constructors, indexers, operators, etc. In .NET, all classes ultimately inherit from the System.Object class, so they are a reference type, that is, when you new an instance of a class, the address of that instance in the managed heap is stored on the stack, and the value of the instance is stored in the managed heap.

What is a struct?

A struct is a value type used to organize a set of related variables into a single variable entity. All structures are inherited from the System.ValueType class, so they are a value type, i.e., the struct instance is created on the stack where the thread is assigned, and it stores the value itself. So when using struct, we can treat it as a basic type class such as int and char.



1. class is the reference type, and structs is the value type

Since class is a reference type, class can be set to null. But we can't make struct null because it's a value type.




2. When you instantiate a class, it will be created on the heap. And you instantiate a struct, which will be created on the stack

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

4. When we pass class as an argument to a method, we pass a reference. struct passes a value, not a reference.

5. structs cannot have initializers, classes can have initializers.





6. Classes can have obvious parameterless constructors, but Structs cannot





7. The new keyword must be instantiated before the class is used, and the Struct does not need it




8. Class supports inheritance and polymorphism, Struct does not. Note: But Struct can implement interfaces just like classes

9. Since Struct does not support inheritance, its members cannot be modified with protected or protected Internal

10. The constructor of Class does not need to initialize all fields, and the constructor of Struct must initialize all fields







11. Class can define a destructor, but Struct cannot

12. Class is suitable for large and complex data, and Struct is suitable for new types that are often used as a combination of some data.



Applicable Occasions: Struct has performance advantages, and Class has object-oriented extension advantages.

The type used for the underlying data store is designed as a Struct type, and the type used to define application behavior is designed as a Class. If you are not sure about the future application of the type, you should use Class.




Previous:Alipay RSA private key and public key one-click generation tool
Next:C# reverse a MySQL help class, save it
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com