Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical differences between classes and structs in .net (not conceptual)?

Whenever I tried to search about differences between classes and structs in C# or .net, I ended up with the conceptual overview of the two things like value type or the reference type, where the variables are allocated etc. But I need some practical differences. I have found some like different behavior of assignment operator, having constructors etc. Can anybody provide some more practical differences which will be directly useful while coding? Like the things works with one but not with other or same operation showing different behavior. And some common mistakes regarding these two.

Also please suggest where to consider using a struct instead of a class. And where the structs should not be used.

Edit: Do I have to call the constructor explicitly or just declaring a struct type variable will suffice?(Should I make it a new question?)

like image 466
Gulshan Avatar asked May 01 '10 13:05

Gulshan


People also ask

What is the difference between classes and structs?

Difference between Structs and Classes: Struct are value types whereas Classes are reference types. Structs are stored on the stack whereas Classes are stored on the heap. Value types hold their value in memory where they are declared, but a reference type holds a reference to an object in memory.

What is the difference between a struct and a class C#?

Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.

What is the difference between a struct and a class in C++ and in C#?

The only different between the class and the structure in C++ is that all of the struct members are public by default, while all the members of the class are private.


1 Answers

OK, here are a few specific, practical differences:

  • A variable can be null if it’s a class, but is never null if it’s a struct.

  • default(T) is null for a class, but for a struct actually constructs a value (consisting of lots of binary zeros).

  • A struct can be made nullable by using Nullable<T> or T?. A class cannot be used for the T in Nullable<T> or T?.

  • A struct always has a public default constructor (a constructor with zero parameters). The programmer cannot override this constructor with a custom implementation — it is basically “set in stone”. A class allows the programmer to have no default constructor (or a private one).

  • The fields in a class can have default values declared on them. In a struct they can’t.

  • A class can inherit from another class, but a struct cannot be declared to derive from anything (it implicitly derives from System.ValueType).

  • It makes sense to use a class in object.ReferenceEquals(), but using a struct variable will always yield false.

  • It makes sense to use a class in a lock() statement, but using a struct variable will cause very subtle failure. The code will not be locked.

  • On a 32-bit system, you can theoretically allocate an array of up to 536,870,912 references to a class, but for a struct you need to take the size of the struct into account because you are allocating actual instances.

like image 90
Timwi Avatar answered Nov 14 '22 21:11

Timwi