Just on other thread at SO today i found this small snippet code which i filled a bit more to make it complete,
class GameObject
{
public virtual void Hello()
{
Console.WriteLine("Hello method in base class");
}
}
class GameObjеct : GameObject
{
public override void Hello()
{
Console.WriteLine("Hello method in derived class");
}
}
class Program
{
public static void Main(string[] args)
{
GameObject obj = new GameObject();//Why i never can call Derived? Though c# allows it?
obj.Hello();
Console.ReadLine();
}
}
Now as per the above code, the CSC shall not warning any thing at all. But the code works surprisingly.
I opened even the IL code using ILDasm, it shows the derived class i.e GameObject as ‘GameObject’ name and the base class name without ”.
So my question is
-
how the compiler is differentiating these both class names plus even at run time?
-
If pasted on notepad with ANSI encoding we get a weird char in the derviced class name as per my friend Abhishek sur. http://twitpic.com/68co6z
Thanks
EDIT: Update on the same code with different name, i am getting error from compiler.
class XXXYYY
{
public virtual void Hello()
{
Console.WriteLine("Hello method in base class");
}
}
class XXXYYY : XXXYYY
{
public override void Hello()
{
Console.WriteLine("Hello method in derived class");
}
}
class Program
{
public static void Main(string[] args)
{
XXXYYY obj = new XXXYYY();//Why i never can call Derived? Though c# allows it?
obj.Hello();
Console.ReadLine();
}
}
Basically the character that looks like “e” in the second classname is a non-ASCII character. If you put that source code into a text file with an encoding which supports it (e.g. UTF-8) and tell the C# compiler what encoding to use (UTF-8 by default, I believe) then it will view them as different class names.
Ignoring the non-ASCII part, this is really just class:
In some fonts you can tell the difference between lower-case-ell and one, but in some you can’t. They’re different characters though.
When you tried to paste it into notepad it failed, because the ANSI encoding you’re using (ANSI is an ambiguous term) doesn’t include that character.
EDIT: Your second snippet doesn’t compile for the obvious reason that you’ve got the same classname declared twice. Not two different names that look the same, but genuinely the same name.