http://mahmoudjameel.blogspot.com/
share what do you think ![]()
-
-
link is not working
-
SecretSoftware wrote:link is not working
Now it's working.
-
abkrino wrote:

SecretSoftware wrote: link is not working
Now it's working.
No, it isn't. You're linking to http:///.
<a href="http://">http://mahmoudjameel.blogspot.com/</a> -
Hopefully now it works
-
It's not any kind of "black magic" or whatever he might think, this doesn't even really involve C# 3.0 (aside from the use of the 'var' keyword). The default implementation of ToString() prints the actual type of the instance, not the type of the variable. Thus his 'xx' variable isn't actually of type 'someclass', it's an Object, just by virtue of the nice things the .NET Framework does for you, it's able to automatically print the type of the instance. You can check this out for yourself using Whidbey if you just replace var with Object. (Oh, and if anyone is wondering how it knows the type of the instance when it's deserialized, it's because it writes both the value and the type information when it's serialized)
Nothing to see here, move along. -
well isn't C# 3.0 is supposed to be stringly typed? that means the compiler must know every type @ compile time?

-
It does know the type, Object. And as an aside, C# in general is strongly typed, not just 3.0.
-
but is prints the type which is someclass
how come? -
Sep102 wrote:
Nothing to see here, move along.
Aaahh..
So you're saying that it's OK to do xx.ToString(), but not xx.dd... that is xx is an Object, not a SomeClass
-
Yep, pretty much. Of course, not having the Orcas CTP installed, I can't actually confirm this, but I'm pretty much sure that's what's happening (especially since I'm basically sure that it can't get any more specific than Object in this case since that's the return type of BinaryFormatter.Deserialize(), which it would be using to infer the type of 'xx').
-
'Cuz an object always know what type it is, even though it is cast into an (Object) and can't use the cc, dd... fieldsabkrino wrote:but is prints the type which is someclass
how come?
-
if that was true you would see system.object on the .tostring() not someclass
correct me if i'm worng -
hmm thanks for the tip

oh and you forgot the return 0 on your snippet
-
Yeah, yeah, too much C++, plus I fixed it when I copied it into VS, it just didn't quite manage to propogate itself back.

-
Nope, try it out for yourself if you're not convinced. For example, if I had
namespace N
{
class C
{
public static void Main()
{
Object o = new C();
System.Console.WriteLine(o.ToString());
}
}
}
The result would be
"N.C"
This, of course, results from the implementation of Object.ToString() being:
public virtual string ToString()
{
return this.GetType().ToString();
}
(As reported by Reflector for .NET)
And GetType() returning the actual type of the instance, not the type of the variable it's being called upon.
Edit: Changed the return type of Main to void, rather than the much more correct System.Int32. -
The method it uses for doing this is commonly called "RunTime Type Information" (RTTI), and it's not something C# invented. Most languages have it, even C++ (although it's not enabled by default on all compilers).
Exactly what you can do with RTTI varies per language. In C#, the runtime type of an object is linked to the assembly metadata, so you can use reflection to do things like get the name of the type as a string (which ToString does; it's default implementation just returns this.GetType().FullName). In C++ there's far fewer information; just enough to use typeof and dynamic_cast, but not much else. -
abkrino wrote:if that was true you would see system.object on the .tostring() not someclass
correct me if i'm worng
No, because your confusing references with instances.
The reference knows what instance its pointed at, just because your reference says what its pointing at is an Object, doesn't mean the instance really is an Object, it just means its castable as one (because ~everything~ in .net inherits from Object.
For example, that new class you just created that doesn't appear to inherit from anything, does actually inherit from System.Object, this is how it gets the 'generic' model members, like ToString and GetType etc.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.