NEWS

Thursday, June 2, 2011

Some basics about C#

I have seen a lot of confusion amongst a lot developers about some basic things of C#. I am going to discuss some of them here.


The very first thing, In C# every thing is a struct or a Class. And every type(Class) is a derived from System.Object, whether is of reference type or Value type.

All Value type variables are derived from System.Value type and System.Value type itself is derived from System.Object. And every value type variables is declared sealed type so no one can derive from it.


Now Lets move some basic Questions.

- What is the difference string, String and System.String?
- What is the difference amongst int, System.Int32, System.Int64.

So these are few basic things, some always have confusion. So lets try to clear it out.

int c;


System.Int32 c;

int c=0;

System.Int32 c = 0;
int c= new int();
System.Int32 c = new System.Int32();

So what is the difference in all above. or Is there any differences?




In one word answer: NO



All of the above would be compiled and will produce the same and exact IL code.



It means all of these are same. Actually C# define primitive types and every primitive is directly mapped to some Class in FCL. So whenever you are going to write the primitive type, compiler while compiling finds the exact Class from the FCL and replaces with it.

Similarly, System.String or String or string all are same. Even I had confusion earlier between String and string. Even when you type both in Visual Studio IDE, both are in different color.

One more thing, I want to say every value type is initialized to its defined default value. Whether you give its default value or not Compiler does it for you, but this is not true for Reference Type.

One already know, writing int is mapped Int32 or is of 32 bit. But some people have different view, according to them int is of 32 bit in 32 bit machines and is of 64 bit on 64 bit machines. Which is absolutely wrong and int is always of 32 bit in C#, whether is of 32 bit or 64 bit machines.

Hope it clears…



No comments:

Post a Comment