views:

55

answers:

4

5-6 years ago when i was in school, I had C as my programming language. And at that time, I read about struct and little bit about Classes.

At that time I had a concept that under struct we cannot declare functions and other such things.

Structure only allows value types and themself are also value types. And I was living under that same old age concepts. 3 days earlier when I had some conversation with my collegues they proved that struct can also contain function definations, constuctors any many more things against my thinking of

public struct  abc
{
   int a;
   long b;
    .... //No function nothing else can come here. Only variables
};

But in .net I saw DateTime Struct and it had functions constructors and everything against my years old concepts.

Then what makes difference between classes and struct if every thing can be done in struct.

I am sure there would be some big differences, due to which struct are still coming with .net rather than obsoleting it.

What are those differences.

And what other such concepts could a one have that changed everything.

A: 

It appears that there is a fundamental difference between a C struct and a .Net struct. According to this MSDN article,

Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

Structs can implement an interface but they cannot inherit from another struct. For that reason, struct members cannot be declared as protected.

Colin Desmond
+1  A: 

Then what makes difference between classes and struct if every thing can be done in struct.

Not everything can be doen in/with structs. Structs are value types, are not accessed through a reference and do not support inheritance.

I am sure there would be some big differences, due to which struct are still coming with .net rather than obsoleting it.

Struct still has some limited use (DateTime is most useful as a Value-type) but an important point is that structs are used rarely. When in doubt, use a class.

Henk Holterman
Structs are sometimes used as a performance optimization, ala XNA.
Merlyn Morgan-Graham
+1  A: 

Everything Colin said, and a few more things:

  • Structs are value types. When you assign a struct to another variable, it makes a copy of the instance. Mutators on the two variables will change two different objects
  • Structs cannot define empty constructors
Merlyn Morgan-Graham
@Merlyn: That's a good catch. I found one big difference mentioned in your first point. And also I am Thanksfull to all who put a light on this
Shantanu Gupta
A: 

As far as I can tell you are asking what are the differences between structs and classes in C#.

The key difference is that structs behave following value type semantics, classes behave according to reference type semantics.

  • Value types directly store a value in a memory location. If you copy a struct, the value gets copied, you now have two values stored in two separate memory locations.
  • Reference types store a value in a memory location and a pointer (or reference) to that memory location. If you copy a class the reference gets copied, but the data remains the same. You now have two references pointing to the same memory location.

Aside from that there are some minor restrictions on what you can put in a struct (such as no parameterless constructors and only interface inheritance), but that isn't what's important. These restrictions are fairly irrelevant really, you shouldn't choose to use a struct just to prevent inheritance. What matters is the how the type behaves semantically and that is what you should focus on when choosing between a struct and a class.

Further information:
This question has some good answers that explain the difference in more detail.
The MSDN page on structs has some good information on the restrictions. Eric Lippart has an interesting two part blog post (part1, part2) on the details of struct vs class.

Simon P Stevens