Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is 'named type'

Tags:

c#

I often come across the term 'named type' in C#. What does it mean?

like image 566
csharpbaby Avatar asked Oct 09 '09 04:10

csharpbaby


4 Answers

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#.

like image 181
Franci Penov Avatar answered Nov 02 '22 23:11

Franci Penov


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).

like image 26
mjv Avatar answered Nov 02 '22 21:11

mjv


Any type which is not a Annonymous Type would be a "Named Type".

like image 45
abhilash Avatar answered Nov 02 '22 21:11

abhilash


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".

like image 2
Cam Soper Avatar answered Nov 02 '22 23:11

Cam Soper