csharpbaby
csharpbaby

Reputation: 6165

what is 'named type'

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

Upvotes: 8

Views: 3672

Answers (4)

mjv
mjv

Reputation: 75125

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

Upvotes: 4

Cam Soper
Cam Soper

Reputation: 1509

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

Upvotes: 2

abhilash
abhilash

Reputation: 5651

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

Upvotes: 2

Franci Penov
Franci Penov

Reputation: 76001

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

Upvotes: 12

Related Questions