I very much suspect this has been requested before, but I can't find it...
Currently, if I want to pass the type of SomeType into the following method:
void DoSomethingWithAType(Type theType) => ...
I must do the following:
DoSomethingWithAType(typeof(SomeType));
As far as I can see, the typeof is pure noise here though and could be removed, by having a reference to a type in isolation imply typeof:
DoSomethingWithAType(SomeType);
Anywhere that a type is specified in isolation (eg, isn't followed by a .), then typeof could be inferred:
var someType = SomeType; // someType is of type, Type.
Seems like a pretty limited gain under a relatively uncommon developer scenario. Even if the syntax isn't ambiguous to the compiler without the operator the typeof(T) makes it very unambiguous to anyone reading the code. Not to mention you'd still need typeof in order to do anything with that Type instance directly which is probably of the more common use cases of working directly with Type.
DoSomethingWithAType<SomeType>(); // tada
I'm still holding out hope, I know I am crazy 馃檴 , that there might someday be first class types in C# someday. If there are this syntax would be more useful there.
This is not just that, this will be horribly ambiguous in case of generics and color-color situations.
This would be a breaking change, since there can be both a type and a variable with the same name in scope (e.g. the Color Color scenario). It would require a change in the name lookup rules to find types where types are currently not accepted.
If you want to continue discussing this idea, please reopen it in the csharplang repo.
Most helpful comment