Can a class property/field be of anonymous type in C# 4.0?

It sounds like you could be asking one or two questions so I'll try and address them both.

Can a class field be strongly typed to an anonymous type

No. Anonymous type names cannot be stated in C# code (hence anonymous). The only way to statically type them is

  1. Generic type inferencee
  2. Use of the var keyword

Neither of these are applicable to the field of a type.

Can a class field be initialized with an anonymous type expression?

Yes. The field just needs to be declared to a type compatible with anonymous types: object for example

public class MyClass { 
  private static object MyProp = new {item1 = "a", item2 = "b"}; 
} 

No, any member should be a strongly typed.

You might go for dynamic type to give your member a chance to be evaluated at runtime though.

Edit : Members should be Explicitly Typed.


In C# 7 you can finally do this:

private (string Login, string Password) _credentials = (Login: "123", Password: "123");