I often come across the term 'named type' in C#. What does it mean?
A type that has been explicitly declared with a name. For example, any class or a struct you declare is a named type.
There are also anonymous types, which are declared without a name and the compiler assigns a name to that type which is not accessible to the developer.
You can read more on the different types of types in C#.
This is in reference to variables that are explicitly typed, as opposed to those which have an anonymous type. Anything that is not an anonymous type is a named type.
Introduced with (I think) .Net 3.0, anonymous types provided a convenient way of creating objects containing a set of read-only properties, without having to define an explicitly named type for that purpose.
Although the implementation of anonymous types also requires the use of type inference (for the typing of the properties of the object), anonymous types are not to be confused with Implicitly Typed Local Variables whereby the var keyword frees the programmer of explicitly stating a variable type, but where a type is effectively provided by the compiler. (most implicitly typed variables are not anonymous types and are therefore a named type. It's just the compiler that does the naming).
Any type which is not a Annonymous Type would be a "Named Type".
A named type is any explicitly defined type (struct, class, etc.) that you create and give a name.
e.g.:
public class Foo
{
public string Bar{ get; set; }
}
In this case, Foo is a named type.
This is as opposed to an anonymous type, which is typically created on the fly:
var MyFoo = new { Bar = "some text" };
I just created a new object, called MyFoo. We haven't explicitly given it a type name, but we have implicitly given it a String property, Bar, with the value "some text".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With