top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between structure and Class in C#?

0 votes
287 views
What is difference between structure and Class in C#?
posted Mar 28, 2017 by Rohini Agarwal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

1)The structures are value types and the classes are reference types.So object of structure store in stack exactly like any other value type like an Integer, a double but object of class store in heap.Your can assigned null to class variable but you can't assigned null value to structure variable

2)Class support Inheritance but struct does not support so access modifier of a member of a struct cannot be protected or protected internal

3)You can use Destructor in a class but You can use it in a structure

4)When passing a class to a method, it is passed by reference. When passing a struct to a method, it's passed by value instead of as a reference.

5)Structs cannot contain explicit parameterless constructors but You can create in class

like

struct a

{

int result;
public b()//it is possible in class, no error will come
{
result = 5;
}
}

struct a

{

int result;
public b()//it is not possible struct,compile error will come
{
result = 5;
}
}

6)You can instance Field initializer of class but You can't do in structs

for example

class emp
{
public string name = "cheater"; //No synstax error
}
struct emp
{
public string name="cheater"; //No synstax error will come
}

7)structures can not be garbage collector so no memory management.
classes can be garbage collector because garbage collector works on heap memory

answer Mar 29, 2017 by Shivaranjini
...