A Struct is a value type typically used to encapsulate small group of related variables.
A structure can have fields, properties, constants, operators, events, methods, constructors, indexers, nested types etc.
A struct can't have parameter less constructor, because CLR initializes all types of struct to their default values by own, so having explicit parameter less constructor will be overhead on performance, and CLR also not guarantees that the constructor will be called.
A structure can have fields, properties, constants, operators, events, methods, constructors, indexers, nested types etc.
A struct can't have parameter less constructor, because CLR initializes all types of struct to their default values by own, so having explicit parameter less constructor will be overhead on performance, and CLR also not guarantees that the constructor will be called.
Ex.
public struct Book
{
public string author;
public string name;
public string publisher;
public decimal price;
}
{
public string author;
public string name;
public string publisher;
public decimal price;
}
Structure Vs Class
A struct is like a class but having some differences -
A. Class is reference type and struct is value type.
B. Class created on heap while struct created on stack.
C. Class can be inherited but struct can't be inherited, A struct can't be a base of any class or struct, because they are sealed by default.
D. Using struct is recommended if having small group of members, because any where we
pass struct a copy of struct passes, so it will be performance overhead if having big size. While class with big group of members is efficient because it's reference passes.
B. Class created on heap while struct created on stack.
C. Class can be inherited but struct can't be inherited, A struct can't be a base of any class or struct, because they are sealed by default.
D. Using struct is recommended if having small group of members, because any where we
pass struct a copy of struct passes, so it will be performance overhead if having big size. While class with big group of members is efficient because it's reference passes.
public struct strEx
{
public int x;
}
{
public int x;
}
public class clsEx
{
public int x;
}
{
public int x;
}
public class clsMain
{
public static void main()
{
strEx ObjStr = new strEx();
clsEx ObjCls = new clsEx();
ObjStr.x = 225;
ObjCls.x = 225;
AssignStr(ObjStr); //A copy of object is passing to method
AssignCls(ObjCls); //Refrence of object is passing to method
Console.WriteLine("Value of x of struct is :{0}", ObjStr.x);
Console.WriteLine("Value of x of class is :{0}", ObjCls.x);
}
public static void AssignCls(clsEx cls)
{
cls.x = 500;
}
{
public static void main()
{
strEx ObjStr = new strEx();
clsEx ObjCls = new clsEx();
ObjStr.x = 225;
ObjCls.x = 225;
AssignStr(ObjStr); //A copy of object is passing to method
AssignCls(ObjCls); //Refrence of object is passing to method
Console.WriteLine("Value of x of struct is :{0}", ObjStr.x);
Console.WriteLine("Value of x of class is :{0}", ObjCls.x);
}
public static void AssignCls(clsEx cls)
{
cls.x = 500;
}
public static void AssignStr(strEx str)
{
str.x = 500;
}
{
str.x = 500;
}
}
Output -
Value of x of struct is 225
Value of x of class is 500
Value of x of struct is 225
Value of x of class is 500
No comments:
Post a Comment