Loading ...

Classes vs Structs | CodeAsp.Net

Classes vs Structs

 /5
0 (0votes)

Classes vs Structs

Structs differ from classes in the way that they are stored in memory and accessed (classes are reference types stored in the heap, structs are value types stored on the stack), and in some of the features (for example, structs don’t support inheritance). You will tend to use structs for smaller data types for performance reasons.

class PhoneCustomer
{
public const string DayOfSendingBill = "Monday";
public int CustomerID;
public string FirstName;
public string LastName;
}


struct PhoneCustomerStruct
{
public const string DayOfSendingBill = "Monday";
public int CustomerID;
public string FirstName;
public string LastName;
}

For both classes and structs, you use the keyword new to declare an instance: This keyword creates the object and initializes it.

PhoneCustomer myCustomer = new PhoneCustomer();      // works for a class
PhoneCustomerStruct myCustomer2 = new PhoneCustomerStruct();// works for a struct

Comments (no comments yet)

Top Posts