top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between struct and classes in c# language?

+1 vote
674 views
What is difference between struct and classes in c# language?
posted May 17, 2014 by Atul Mishra

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

2 Answers

0 votes

Structs differ from classes in several important ways:
- Structs are value types.
- All struct types implicitly inherit from the class System.ValueType.
- Assignment to a variable of a struct type creates a copy of the value being assigned.
- The default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null.
- Boxing and unboxing operations are used to convert between a struct type and object.

The meaning of this is different for structs.
- Instance field declarations for a struct are not permitted to include variable initializers.
- A struct is not permitted to declare a parameterless instance constructor.
- A struct is not permitted to declare a destructor.

answer Jul 17, 2014 by Krishna
0 votes

1.Structures are value types and the classes are reference types.
2.Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
3.Classes can be inherited whereas structures not.
4.A structure couldn't be null like a class.
5.A structure couldn't have a destructor such as a class.
6.A structure can't be abstract, a class can.
7.You cannot override any methods within a structure except the following belonging to the type object:
Equals()
GetHashCode()
GetType()
ToString()
And the other polymorphism technique used for structures is implementing interfaces.
8.Declared events within a class are automatically locked and then they are thread safe, in contrast to the structure type where events can't be locked.
9.A structure must always have the default parameter less constructor defined as public but a class might have one, so you can't define a private parameter-less constructor as in the following:

  struct Me
        {
            private Me()// compile-time error
            {
            }
        }

    class Me
             {
                private Me()// runs Ok{
             }

10.A static constructor is triggered in the case of a class but not in the case of a structure as in the following:

struct myStructure
{
static myStructure()
{
Console.WriteLine("This is me a structure");
}
}
class myClass

{
    static myClass()
    {
        Console.WriteLine("This is me a class");
    }
}

class Program
{
    static void Main(string[] args)
    {
       myStructure s =new myStructure();//Nothing happen
       myClass c =new myClass();//Will out put This is me a class
       Console.Read();
    }
}

11.The strucutre can't conatain a volatile field whereas the class can
12.You can't use sizeof with classes but you can with structures
13.Fields are automatically initialized with classes to 0/false/null wheatheras strucutres are not
14.Fields can't be directley instantiated within structures but classes allow such operations as in the following:

 struct myStructure
        {
            publicstring x = 2;//Not allowed
        }
        class myClass
        {
            publicstring x = 2;//Allowed
        }

15.Structures and classes don't adopt the same aproach for the System.Object.Equals() method.

Assume the following strucutre and class:

struct StructurePerson
    {
        publicstring FirstName;
        publicstring LastName;
    }
    class ClassPerson
    {
        publicstring FirstName;
        publicstring LastName;
    }

Now, try this code:

class Program
    {
        static void Main(string[] args)
        {
            StructurePerson strX =new StructurePerson();
            strX.LastName = "Bejaoui";
            strX.FirstName = "Bechir";
            StructurePerson strY =new StructurePerson();
            strY.LastName = "Bejaoui";
            strY.FirstName = "Bechir";

            if (strX.Equals(strY))
            {
                Console.WriteLine("strX = strY");
            }
            else
            {
                Console.WriteLine("strX != strY");
            }//This code displays strX = strY

            ClassPerson clsX =new ClassPerson();
            clsX.LastName = "Bejaoui";
            clsX.FirstName = "Bechir";
            ClassPerson clsY =new ClassPerson();
            clsY.LastName = "Bejaoui";
            clsY.FirstName = "Bechir";

            if (clsX.Equals(clsY))
            {
                Console.WriteLine("clsX = clsY");
            }
            else
            {
                Console.WriteLine("clsX != clsY");
            }//This code displays clsX != clsY
            Console.Read();
        }
    }

In the first strucutre the two objects are value types, they are compared depending on their values like int I = 5 and int J = 5 so I=J because they have the same value. In the contrast, in the class case of two different and distinct references, to make clsX = clsY you should use the following code:

ClassPerson clsX =new ClassPerson();
            clsX.LastName = "Bejaoui";
            clsX.FirstName = "Bechir";
            ClassPerson clsY = clsX;
            if (clsX.Equals(clsY))
            {
                Console.WriteLine("clsX = clsY");
            }
            else
            {
                Console.WriteLine("clsX != clsY");
            }//This code displays clsX = clsY
answer Nov 18, 2014 by Manikandan J
...