8

Greetings Overflowers,

I know in C we can define a struct inline with the variable declaration so that the struct type is specific to this variable. This is instead of defining the type alone then declaring the variable to be of that struct type. Is this possible in C#?

Thank !

binki
  • 7,754
  • 5
  • 64
  • 110
geeko
  • 2,649
  • 4
  • 32
  • 59
  • 9
    I was expecting to actually see some C code. Alas, the expectation has not been duly met, and I am thus overcome with great disappointment. – BoltClock Apr 07 '11 at 09:46
  • An example would be good. Also, why do you need to do this, perhaps there is another way of solving the problem in C#? – Steve Apr 07 '11 at 09:47
  • 4
    Yes, it is possible in C#, but it is written like this. – Kobi Apr 07 '11 at 09:47
  • It is a common assumption that C# should resemble C. It doesn't, you have to look beyond the {braces}. Even C++ hasn't adopted the borked C struct syntax. Coming up with a type name for the struct is a small effort. There are plenty of other places where C# *forces* you to do the Right Thing. – Hans Passant Apr 07 '11 at 12:10
  • This is desirable when declaring a struct type only ever used in one array which is filled at compile-time as a sort of look-up table. The example C: `const struct { const char *name; int (*action)(human_t *, int argc, char *argv[]); } actions[] = { {.name = "eat", .action = action_eat}, {.name = "sit", .action = action_sit}, {.name = NULL}, };` – binki Jul 15 '13 at 16:43

3 Answers3

17

This is not possible in C#, however you can define an instance of an anonymous type like this:

var x = new { SomeField = 1, SomeOtherField = "Two" }; 

This would effectively be the same, giving you an instance of a type that is specific to that variable and cannot used outside the variable's scope.

Avish
  • 4,516
  • 19
  • 28
  • 1
    **Note:** anonymous types can't be returned from or passed to other methods, i.e. they only exist in the scope of the method they have been defined in. It is not possible to have a variable of an anonymous type on class level. – Daniel Hilgarth Apr 07 '11 at 09:48
  • 1
    @Daniel: True, although I'm pretty sure the same or equivalent restrictions apply to anonymous structs in C, and indeed to any statically-typed language. "If it doesn't have a name, you're not allowed to mention it". Makes sense. – Avish Apr 07 '11 at 09:52
  • @Daniel - Actually, you can pass it to a generic method, but it is very ugly. It will also be shared with *other* anonymous types with the same fields, but again, great ugliness. But obviously, not in a class level - if C++ allows that, I don't think C# has anything similar. – Kobi Apr 07 '11 at 09:52
  • @Kobi: You are right. Actually, I used to know that, but forgot about it, because it really doesn't make any sense to do it :-) – Daniel Hilgarth Apr 07 '11 at 09:56
  • Another minor downside is the type is not mutable - annoying if you're constructing it and need to initialise some member variables later. As for moving it out of scope... well the evil of `dynamic` has changed the landscape somewhat, but from a design point of view I think we all agree this is generally a terrible compromise – fostandy Oct 26 '16 at 08:28
2

Simple answer: No, it is not possible.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
2

Would this not help ?

(string, string, int) retVal;
retVal.Item1 = "A String";
retVal.Item2 = "Another String"
retVal.Item3 = 4;
spooch
  • 21
  • 1
  • 3
  • 1
    This should be better: ``` (string s1, string s2, int integer) retVal; retVal.s1 = "A String"; retVal.s2 = "Another String" retVal.int = 4; ``` – Tyler Temp May 19 '22 at 07:50