C# constants and variables troubleshooting

The program appears "the constant value 267 cannot be converted to byte" as follows:

namespace Test01
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            float fl = 3.14F;
            double d = 3.14D;
            fl = (float)d;
            int ls = 927;//声明一个int类型的变量ls
            byte shj = 257;//此处发生错误
            Console.WriteLine("ls={0}", ls);//输出int类型变量ls
            Console.WriteLine("shj={0}", shj);//输出byte类型变量shj
            Console.ReadLine();
        }
    }
}

Change the value of the constant shj to: 0~255
Note:
When defining the type of the variable, you must be clear about the value range of the type.

The program has an "unexpected character ';' error message" as follows:

namespace Test02
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int a=368//此处发生错误
            Console.WriteLine(a.ToString());//输出变量a的值
            Console.ReadLine();
        }
    }
}

Change Chinese ";" to English ";" to
strictly distinguish between Chinese and English symbols in the C# program.

The program appears "cannot implicitly convert double type to float type; please use F suffix to create this type" error message, the code is as follows:

namespace Test03
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            float f = 15.5;//此处发生错误
            Console.WriteLine("float变量值为:" + f);
            Console.ReadLine();
        }
    }
}

You should add f or F after 15.5
to define the variable type, pay attention to its usage rules, and don't violate the rules, otherwise the program will report an error.

There is an error message "Cannot implicitly convert type double to type int. There is an explicit conversion (is there a missing cast?)" in the program, the code is as follows:

namespace Test04
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            double a = 1987.0112;//声明double类型变量x
            int b = a;//此处有错误
            Console.WriteLine("double变量值:" + a);//输出double变量x
            Console.WriteLine("int变量值:" + b);//输出整型变量y
            Console.ReadLine();
        }
    }
}

should be changed to this:

int b = (int)a;

Add a cast.

The program displays the error message "The conversion specified by InvalidCastException was not handled is invalid", the code is as follows:

namespace Test05
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            double a = 208;//声明一个double类型变量db,并初始化为2008
            object obj = a;//对db变量执行装箱操作
            Console.WriteLine("1、db的值为{0},装箱之后的对象为{1}", db, obj);
            int b =(int) obj;//拆箱操作,此处有错误
            Console.WriteLine("2、obj的值为{0},拆箱之后的对象为{1}", obj, i);
            Console.ReadLine();
        }
    }
}

It is mainly caused by the inconsistency of the data types after boxing and unboxing. You can replace the int type with the double type as follows:

double b =(double) obj;

The program has an error message "The left side of the assignment number must be a variable, attribute or indexer", the code is as follows:

namespace Test06
{
    
    
    class Program
    {
    
    
        const double PI = 3.14;//声明一个常量
        static void Main(string[] args)
        {
    
    
            PI = 3.14926;//此处错误
            Console.WriteLine(PI);
            Console.ReadLine();
        }
    }
}

The value of the constant cannot be changed, you can remove this line of code, or you can remove the const keyword in the variable declared above.

The program appears "cannot use the instance reference to access the member; please use the type name to qualify it" error message, the code is as follows:

namespace Test07
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            Test test=new Test();//创建类的对象
            Console.WriteLine("常量的值:"+test.PI);//输出常量的值,此处发生错误
            Console.ReadLine();
        }
    }
    class Test
    {
    
    
        public const double PI = 3.14;
    }
}

The constant needs to be called directly using the class name (same as the static member), and cannot be called using the object of the class and changed to the following code;

namespace Test07
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            
            Console.WriteLine("常量的值:"+ Test.PI);//输出常量的值,此处发生错误,常量需要直接使用类名进行调用(与静态成员一样),不能使用使用类的对象调用
            Console.ReadLine();
        }
    }
    class Test
    {
    
    
        public const double PI = 3.14;
    }
}

The above is my summary of some basic mistakes that variables and constants in C# are prone to.

Guess you like

Origin blog.csdn.net/weixin_63284756/article/details/128434356