tags:

views:

120

answers:

6

As in:

public class MyClass {

  private static var MyProp = new {item1 = "a", item2 = "b"};

}

Note: The above doesn't compile nor work (the var cannot be used there), it's only to show my point.

Update: Just to clarify the question, I had already tried using

private static dynamic MyProp = new {item1 = "a", item2 = "b"};

and this works, but it doesn't generate intellisense because of the dynamic typing. I am aware that anonymous typing is just a compiler trick, so I hoped I could use this trick to my advantage by declaring a structured field without having to declare a class beforehand (mainly because there's only going to be one instance of this particular kind of field). I can see now that it's not possible, but I'm not sure why that is. If the compiler is simply generating an implicit type for an anonymous object, it should be fairly simply to have the compiler generate this implicit type for a field.

+2  A: 

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"}; 
} 
JaredPar
Err...so the answer is not "yes", but "no"?
Adam Robinson
@Adam the answer is the question is ambiguous :) I'll try to make my answer less so
JaredPar
Yes... Both object and dynamic does not give an anonymous member. So Answer should be no.
abhishek
Curious; If you assign an anonymous type to an `object`, how do you cast it to get the real type?
Andrew Barber
@abhishek: `object` is certainly a type.
Adam Robinson
@Andrew: Unicorn magic. Other than that, you can't.
Adam Robinson
@Adam, sorry, I mistakenly wrote that. I meant something else. So edited.
abhishek
I think you might go for either object or dynamic, but in both the cases you need to employ reflection as the type is missing to you.
abhishek
If you use object as the field's type, you lose any ability to access the 2 properties of that type without reflection (which defeats the whole purpose of this)
Diego
@Diego, When i read the question I couldn't tell which scenario you were getting at and tried to offer the most complete answer. There are tricks othre than reflection to get at an anonymous types values from `object` but they're hacky IMHO. So yes it does rendere the object mostly less than useful. If you need a field the best path is to use a defined type or tuple.
JaredPar
+3  A: 

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.

abhishek
var is strongly typed. I think you what you mean to say is "explicitly typed".
Mark
@Mark Yes, var cannot be used as a member for a class.
abhishek
A: 

No, anonymous types cannot be exposed outside of their declaring function as anything other than object. While the anonymous type is just syntactical sugar and does, in fact, create a concrete type, this type is never known (nor available) to the developer, and thus can't be used.

Adam Robinson
You say "never known" and "can't be used," but aren't you discounting reflection and `dynamic`? (Not that I'm encouraging the practice, mind you.)
Anthony Pegram
@Anthony: No, neither of those allow the developer (as in, the person writing the code) to "know" the type. You can certainly call `GetType()` on the object, but that is a runtime-only operation. Nor is `dynamic` a solution.
Adam Robinson
+2  A: 

If this is C# 4, look into the dynamic keyword.

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

However, as soon as you do this you lose any sort of type-safety and handy compiler checks.

Joshua Rodgers
That's what I did, but I lost intellisense on the object's properties. I'd like to do it so that intellisense can aid people using my class.
Diego
+1  A: 

How about using Tuple<string, string> instead?

public class MyClass {

  private static Tuple<string, string> MyProp = Tuple.Create("a", "b");

}
Dr. Wily's Apprentice
I was about to provide this exact code block myself. Explicitly statically typed, and you don't have to manually construct a new type. I really hope this gets upvoted ahead of those silly "dynamic" solutions.
Mark
Of course, now that your answer was sniped that is probably unlikely.
Mark
Hehe, oh well. It's not necessarily clear anyway from the question that a Tuple would be a good solution. The example code only has fields "item1" and "item2", but perhaps the real code has more logical names, which would be lost by using a Tuple. Since this is a private field a Tuple is probably ok, but if this were a public field I'd probably say a better idea is to define a specific type to store this data, even though it's a bit more coding.
Dr. Wily's Apprentice
Exactly, the idea is to have a static constant field that is structured, and could have several levels of nesting anonymous components.
Diego
+1  A: 

An property (or field) of a named class can't have an anonymous type, because such a type can't be referred to in code. However, an anonymous type can contain properties of anonymous type:

var myObj = new
{
    Foo = new { Name = "Joe", Age = 20 }
};

But such a construct has no practical use outside the local scope...

Thomas Levesque