Several C# Language Questions

Posted by Water Cooler v2 on Stack Overflow See other posts from Stack Overflow or by Water Cooler v2
Published on 2010-05-20T23:27:04Z Indexed on 2010/05/21 5:10 UTC
Read the original article Hit count: 173

1) What is int? Is it any different from the struct System.Int32? I understand that the former is a C# alias (typedef or #define equivalant) for the CLR type System.Int32. Is this understanding correct?

2) When we say:

IComparable x = 10;

Is that like saying:

IComparable x = new System.Int32();

But we can't new a struct, right?

or in C like syntax:

struct System.In32 *x;
x=>someThing = 10;

3) What is String with a capitalized S? I see in Reflector that it is the sealed String class, which, of course, is a reference type, unlike the System.Int32 above, which is a value type.

What is string, with an uncapitalized s, though? Is that also the C# alias for this class?

Why can I not see the alias definitions in Reflector?

4) Try to follow me down this subtle train of thought, if you please. We know that a storage location of a particular type can only access properties and members on its interface. That means:

Person p = new Customer();
p.Name = "Water Cooler v2"; // legal because as Name is defined on Person.

but

// illegal without an explicit cast even though the backing 
// store is a Customer, the storage location is of type 
// Person, which doesn't support the member/method being 
// accessed/called.
p.GetTotalValueOfOrdersMade();

Now, with that inference, consider this scenario:

int i = 10;

// obvious System.object defines no member to 
// store an integer value or any other value in. 
// So, my question really is, when the integer is 
// boxed, what is the *type* it is actually boxed to. 
// In other words, what is the type that forms the 
// backing store on the heap, for this operation?
object x = i;

Update

Thank you for your answers, Eric Gunnerson and Aaronought. I'm afraid I haven't been able to articulate my questions well enough to attract very satisfying answers. The trouble is, I do know the answers to my questions on the surface, and I am, by no means, a newbie programmer.

But I have to admit, a deeper understanding to the intricacies of how a language and its underlying platform/runtime handle storage of types has eluded me for as long as I've been a programmer, even though I write correct code.

© Stack Overflow or respective owner

Related posts about c#

Related posts about language-features