0

I'm reading the C# Docs and I've encountered these sentences:

although a namespace name is classified as an expression, it does not evaluate to a value and therefore can never be the final result of any expression. You cannot pass a namespace name to a method parameter, or use it in a new expression, or assign it to a variable. You can only use it as a sub-expression in a larger expression. The same is true for types (as distinct from System.Type objects), method group names (as distinct from specific methods)

What exactly does "The same is true for types (as distinct from System.Type objects)" mean? Can you give me an example of this with System.Type? Does it have something to do with reflection?

Also what are "method group names" and "specific methods"?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
BoSsYyY
  • 563
  • 5
  • 13
  • 1
    Most of the things you mentioned can only appear in an expression as the left operand of a member access expression (the dot `.`) Types can, in addition to being the left operand of a member access (to reach static members) also appear inside of cast notation, with `is`, `as`, and `typeof` operators, and as actual generic parameters. – Ben Voigt Feb 18 '18 at 05:59
  • 1
    No, `System.Type` objects are what you get when you use the `typeof` operator. A type name or placeholder goes in, a `System.Type` instance comes out. – Ben Voigt Feb 18 '18 at 06:02
  • "Method group names" already answered here. https://stackoverflow.com/questions/886822/what-is-a-method-group-in-c – Wyck Feb 18 '18 at 06:06
  • This seems to be an English question rather than a coding question. It is simply worded to clarify that they are not talking about `System.Type` when they say types, but actual defined types such as classes, structs, etc. – ProgrammingLlama Feb 18 '18 at 06:10

1 Answers1

2

It means that not all expressions start out as values. Some expressions are namespaces or types.

Consider what might appear on the right side of:

var foo = some_expression;

Most expressions would stand in well for some_expression because most expressions are values. But some expressions are not. Such as int or System

var foo = int; // ERROR

Yet types can be part of an expression that is a value:

var foo1 = 42 is int; // foo1 is a bool
var foo2 = typeof(int); // foo2 is a System.Type object
var foo3 = default(int); // foo3 is an int

Similarly, namespaces like System aren't values, but they are expressions. You can use namespace expressions to get other expressions - for example, using the dot . operator to form a type expression. System.String is an expression that is a type. And, as before, you might further write it as typeof(System.String) to get an expression that is an actual value that you could assign to a variable.

I suggest looking at Expression trees, as used in LINQ or Lambda expressions, or the way you craft expressions when emitting IL. That might really help you get a handle on how the language (and the compiler) treat expressions and why the grammar is defined the way it is.

https://msdn.microsoft.com/en-us/library/system.linq.expressions(v=vs.110).aspx

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expressions

Wyck
  • 10,311
  • 6
  • 39
  • 60