简体   繁体   English

带有 var 的匿名类型给出错误 CS0825

[英]Anonymous type with var gives error CS0825

I want to create an anonymous type in C# inside a class.我想在一个类中用 C# 创建一个匿名类型。

The examples I have seen use var to create an anonymous variable我见过的例子使用var来创建一个匿名变量

    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:但是将我的匿名类型声明为 var 会触发以下错误:

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?匿名变量的类型是什么,所以我不必使用 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.我明白错误告诉我什么,但我不想在函数内移动我的变量,它需要是 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).编辑: enum 是我一开始尝试的,但我需要这些值比整数更灵活(比如字符串,所以我可以转储 jon 文件)。 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. var (根据定义匿名类型)只能在方法内声明,错误消息基本上是在告诉你。 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.请注意,我还使用了static因此您无需创建该类的新实例,但如果您认为它相关,请更改它。

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.如果你想要字符串,你可以创建自己的类,就像上面一样。

You can use the dynamic type to have an anonymous instance variable.您可以使用dynamic类型来拥有匿名实例变量。

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 .有关使用强类型对象的替代方法,请参阅DavidG回答,该对象不需要您将代码暴露给与dynamic类型相关许多问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM