0

I want to create an anonymous type in C# inside a class.

The examples I have seen use var to create an anonymous variable

    var RecordId = new 
    { 
        Foo = 0,
        Bar = "can be also a string"
    };

However I want to create my anonymous variable inside a class.

public class Logger //: LogBase
{
    var RecordId = new 
    { 
        Foo = 0,
        Bar = 1
    };
}

So when Logging I can do:

Logger.RecordId.Foo

But declaring my anonymous type as var triggers the following error:

CS0825: The contextual keyword 'var' may only appear within a local variable declaration.

What is the type of an anonymous variable, so I don't have to use var?

I understand what the error is telling me, but I don't want to move my variable inside a function, it needs to be a property of Logger.

Edit: enum is what I tried t the beginning, but I need the values to be more flexible than just integers (like strings, so I can dump jon files). I updated my question to reflect that.

  • `var` (and by definition anonymous types) can only be declared inside a method, the error message is basically telling you that. If you need this type to be at class level, then make a class/struct/tuple to store it. – DavidG Oct 26 '21 at 17:44
  • @DavidG could you put an example? With dictionaries it gets closer to what I want, but I want to access each RecordId with the dot notation `Logger.RecordId.Foo` instead of `Logger.RecordId["Foo"]`, so the IDE shows the available RecordIds –  Oct 26 '21 at 17:49
  • *What is the type of an anonymous variable, so I don't have to use var?* It's written by the compiler based on the types of the members; it's not something you can write on your own code.. And to be honest it would make stuff a bit of a mess if you tried (pause code in a debugger that hs an AT in context and look at the type reported by the debugger) If you're looking for quicker ways to declare types, look at tuples and records.. But in this case I agree with Daniel; appears you're looking for an enum.. it's hard to tell exactly with the foo/bar contrivance though. Real example please – Caius Jard Oct 26 '21 at 17:49
  • What does "dumping a Jon file" entail? Is it something to do with Json? – Caius Jard Oct 26 '21 at 18:10

3 Answers3

1

var (and by definition anonymous types) can only be declared inside a method, the error message is basically telling you that. If you need this type to be at class level, then make a class/struct/tuple to store it.

public static class Record
{
    public static int Foo { get; set; }
    public static int Bar { get; set; }
}

public class Logger //: LogBase
{
    public static Record RecordId { get; set; } = new Record();
}

Now you can do this:

var foo = Logger.RecordId.Foo;

Note that I also used static so you don't need to create a new instance of the class, but change that if you think it's relevant.

DavidG
  • 113,891
  • 12
  • 217
  • 223
-1
public class Logger //: LogBase
{
    public enum RecordId 
    { 
        Foo = 0,
        Bar = 1
    }
}

If you do not want strings you can do the above.

public class LogCategory
{
    private LogCategory(string value) { Value = value; }

    public string Value { get; private set; }

    public static LogCategory Foo { get { return new LogCategory("Foo"); } }
    public static LogCategory Bar { get { return new LogCategory("Bar"); } }

}

If you want strings you could create your own class something like the above.

Daniel Kelsch
  • 393
  • 2
  • 10
  • I tried with enum, but enums only allow the value to be numeric. I need that tha value can be anything, like a string, and this is not permitted in enums. I updated my question –  Oct 26 '21 at 17:44
-2

You can use the dynamic type to have an anonymous instance variable.

public class Foo
{
    dynamic bar = new {
        A = 1,
        B = 2
    };
    
    public void Print() {
        Console.WriteLine(bar.A);   
    }
}

Try it out!


Just because you can do this doesn't mean it's a good idea. See DavidG's answer for an alternative using a strongly-typed object that will not require you to expose your code to the many problems associated with the dynamic type.

D M
  • 5,769
  • 4
  • 12
  • 27