struct and classes

08/02/2012 08:17 Mind Fuck#1
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
Code:
    struct time
    {
        private int x = 5; // compile-time error        
    }
Structs cannot contain explicit parameterless constructors
Code:
    struct time
    {
        private time() // compile-time error
        {
        }
    }
at struct all Fields must be fully assigned before control is returned to the caller
Code:
    struct time
    {
        private int x; // compile-time error
        public time(int a)
        {
        }
    }
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

Code:
struct time
    {
        private int x = 10; // compile-time error
    }
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

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;
    }
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 :)
08/02/2012 08:46 I don't have a username#2
Shit I'm dumb. I was about to sum up the errors in your thread and I realize it was a tutorial.
08/02/2012 08:54 InfamousNoone#3
You should probably actually learn what you're trying to write a tutorial about before writing a tutorial about....
08/02/2012 11:12 Mind Fuck#4
Quote:
You should probably actually learn what you're trying to write a tutorial about before writing a tutorial about....
so that was horrible ?
08/02/2012 11:26 InfamousNoone#5
[Only registered and activated users can see links. Click Here To Register...] provides more insight than your post.
08/02/2012 11:52 Mind Fuck#6
i wasn't explaining the struct and classes ? , i was pointing out some difference between them and i assume who read this already knows about struct and classes
anyway thanks for the feed back
08/03/2012 00:11 InfamousNoone#7
If they already know about them, why wouldn't you assume they know the differences between them...? lol.
Secondly, you never mention why certain limitations are imposed onto structs, you just say "here is the difference": (example) -- which is useless.