繁体   English   中英

在类中声明静态变量

[英]declaring static variable in the class

在Student类中将collName声明为静态变量时出现错误。 谁能帮我找到它。

class Student
    {
        public int SId;
        public string SName;
        public Student(int SId)
        {
            this.SId = SId;
            Console.WriteLine("Constructor is called");
        }
        static string coll = new string ();
        cool = "RVIM"

我正在错误以下。

Error   1   Invalid token '=' in class, struct, or interface member declaration C:\Users\amruthap\AppData\Local\Temporary Projects\ConsoleApplication1\Student.cs   18  14  ConsoleApplication1
Error   2   'string' does not contain a constructor that takes 0 arguments  C:\Users\amruthap\AppData\Local\Temporary Projects\ConsoleApplication1\Student.cs   17  37  ConsoleApplication1
Error   3   'ConsoleApplication1.Student.coll' is a 'field' but is used like a 'type'   C:\Users\amruthap\AppData\Local\Temporary Projects\ConsoleApplication1\Student.cs   18  9   ConsoleApplication1
    }

您无法调用默认的字符串构造函数- 字符串类没有带0参数的ctor。请尝试将coll设置为“”

字符串不包含带有0个参数的构造函数

第二件事cool = "RVIM" =类struct或接口成员声明中的无效令牌。

不要调用字符串构造函数...尝试将其视为值类型

静态字符串coll =“”;

暂无
暂无

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

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