views:

170

answers:

4

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.

+1  A: 

1) Yes. "int" is just an alias that C# defines for System.Int32.

2) Your first answer.

3) string is the C# alias for the type System.String. Since nearly everybody has "using System;" in their program, you can either use "string" or "String".

4) You can think of it as an int stored in a reference type box. Even though the type of the box is only visible as "object", the runtime knows that there's an int inside of it.

That's why you can't write:

int i = 10;
object x = i;
short j = (short) x; // this is an error because you can only unbox x as an int.

Much of this is in the C# language reference or in one of the introductory books.

Eric Gunnerson
+5  A: 
  1. int is an alias for System.Int32. The types are identical.

  2. IComparable x = 10 would be similar to writing var i = 10; IComparable x = i. The compiler chooses what it thinks is the most likely type for the constant, then does an implicit conversion to IComparable.

  3. string is an alias for System.String, similar to #1. You can't see the alias definitions in Reflector because the alias is part of the C# compiler, not the .NET Framework itself. (It's different in VB.NET, for example.)

  4. A boxed integer or other value type is a reference to the value. You could probably think of it as a pointer with some type information attached. The actual backing type, however, is simply System.Object.

Aaronaught
+1 Good answers!
Andrew Hare
+2  A: 
Dan Tao
Thanks for stimulating the discussion, Dan. You're now bordering on the territory where I see the horizon. Sadly, though, I'm going to need to get some sleep before I can resume an intelligent conversation on the subject. The dawn's just broken in my part of the planet, and I've been awake too long.I hope to continue this discussion, though.
Water Cooler v2
@Water Cooler v2: I have added a lot to my answer to make the explanation as clear as possible. Hopefully I haven't just made the matter more confusing. Take a look and let me know if you have more questions.
Dan Tao
A: 

1) int is an alias for the structure System.Int32. You can define any alias you want for a type in C#. For doing that you need to use an using statment similiar to the using statments you normally to import namespaces. For example, if I'd like to create an alias to System.Int64 and call it number, I'd write the following using statment at the beginning of my code file:

using number = System.Int64;

Then, every time I use the alias number on my code the compiler and the intellisense will treat it exactly as System.Int64.

2) Use the default constructor of an System.Int32 is the same as assign 0 to an integer variable.

IComparable x = new System.Int32();

does exaclty the same as the code

IComparable x = 0;

It is possible to use the new operator with structures. The semantics of the new to allocate the memory necessary for an object and invoke constructors. The diference between an object defined as a struct and an object defined as a class is how it is allocated. Struct intances are allocated at the stack, whereas the class instances are allocated at heap.

Some interesting fact is the in C# not everything derivates from object.

3) string is keyword of C# compiler (as the int keyword) that is why you cannot see its definition using Reflector. In fact, the IL code generated by the compiler doesn't even now the existance of those alias, because they are used only by the compiler during the compilation process. After the compilation all string references become a System.String in your compiled code.

4) The answer for this question is too long, so I suggest you to read the following article: Representation and Identity

Carlos Loth