I’m reading book “C# Language”, and hit this example from Page 123-124:
The meaning of a name within a block may differ based on the context in which the name is used.
In the example
using System;
class A { }
class Test
{
static void Main()
{
string A = "hello, world";
string s = A; // Expression context
Type t = typeof(A); // Type context
Console.WriteLine(s); // Writes "hello, world"
Console.WriteLine(t); // Writes "A"
}
}
the name A is used in an expression context to refer to the local variable A and in a type
context to refer to the class A.
I’m fine with the visibility of class A. However, here (Type t = typeof(A)) class A preceded string A.
So, what is the “priority” or “sequence” of matching/choosing a possible “A”?
There’s no conflict.
typeofonly works on class names. To get the Type of an object instance, you use.GetType().