took about 50 pages reading about difference between structs and classes , so i thought it's good idea to sum up this for whoever want to get it with a spoon , feel free to correct or add information
at struct cannot have instance field initializers in structs
Structs cannot contain explicit parameterless constructors
at struct all Fields must be fully assigned before control is returned to the caller
struct is value type and class is ref. type
struct use values so it's in stack but type use ref so it's in heap
can't declare a defualt constructor at struct but u can at class
at struct it will generate a default const. for u even if u got one but it won't at class
struct won't intialize a field for u at ur constructor but class will
at struct you are not allowed to initialize instance fields at there point of decleration(but u still need to assigned value to it before it return to the caller) but u can at class
what i said doesn't cover inheritance , please feel free to tell me why should i use one and not the other while codding something
one more info
at struct you can declare a field witout new keyword (as struct is values) but once u try to access it , it will just give an error , but with new keyword you declare them with defualt values aka 0 , false and null , here is an example
at both cases the time feild (variable) will be created on stack but at the first it will be left in their uninitialized state and any attempt to access them will result to compile-time error
if u found it helpful lemme know to give it a try at arrays and collections , im reading tons of pages and sum up for my self in lines as refrence and got no problem to share them with u :)
at struct cannot have instance field initializers in structs
Code:
struct time
{
private int x = 5; // compile-time error
}
Code:
struct time
{
private time() // compile-time error
{
}
}
Code:
struct time
{
private int x; // compile-time error
public time(int a)
{
}
}
struct use values so it's in stack but type use ref so it's in heap
can't declare a defualt constructor at struct but u can at class
at struct it will generate a default const. for u even if u got one but it won't at class
struct won't intialize a field for u at ur constructor but class will
at struct you are not allowed to initialize instance fields at there point of decleration(but u still need to assigned value to it before it return to the caller) but u can at class
Code:
struct time
{
private int x = 10; // compile-time error
}
one more info
at struct you can declare a field witout new keyword (as struct is values) but once u try to access it , it will just give an error , but with new keyword you declare them with defualt values aka 0 , false and null , here is an example
Code:
class Program
{
static void Main(string[] args)
{
time now ;
Console.WriteLine(now.hr); // compile-time error
}
}
struct time
{
public int hr, min, sec;
}
Code:
class Program
{
static void Main(string[] args)
{
time now = new time();
Console.WriteLine(now.hr); // no errors , result is 0
}
}
struct time
{
public int hr, min, sec;
}
if u found it helpful lemme know to give it a try at arrays and collections , im reading tons of pages and sum up for my self in lines as refrence and got no problem to share them with u :)