Fen*_*III 1 c# blazor blazor-server-side
我正在学习 Blazor 和 C#(两者都是新的),并且正在玩我的一个宠物项目。
对于该项目,我编写了一个Project.cs文件,位于按照本Data教程创建的结构的目录中。
在某些时候,我需要一个字典数据结构,我尝试在类中创建这样的数据结构:
namespace MyApp.Data;
public class Project
{
public Project()
{
}
Dictionary<string, string> openWith =
new Dictionary<string, string>();
// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
}
Run Code Online (Sandbox Code Playgroud)
但是当我直接Invalid token '(' in class, record, struct, or interface member declaration从 Microsoft文档中获取这些行时,我收到了错误
我究竟做错了什么 ?
你不能在类中运行任何代码。您可以将代码移至构造函数内部或特殊方法内部,也可以直接使用 {} 初始化属性。
public class Project
{
Dictionary<string, string> openWith =
new Dictionary<string, string>();
public Project()
{
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
}
}
Run Code Online (Sandbox Code Playgroud)