c# - 常量定义与赋值

1.前言

c#与Java很相似,但是不一样,又与js(JavaScript)相似,但是也不一样,所以我认为c#是Java和 js的孩子。

2.常量定义

字符串:

const string = "love";

是的,没错,数据类型就是小写,常量数据还需要带修饰符const,与js 的 cs6语法类似

 

3.基本数据类型的定义与赋值如下:

整数类型:
   const int b = 3;
单精度浮点型:
const float c = 4.56f;
双精度浮点型:
const double d = 5.67889;
字符型:
const char f = 'f';
boolean型:
const bool g = true;
短整数数据类型:
const short h = 343;
长整数类型 :
const long i = 123456789;
二进制类型:
byte[] j = System.Text.Encoding.Default.GetBytes ( "33333333333" );

注意了,不加const

是不是很不一样?

那么,可以使用一个通用的数据类型 var  ,与js一样

 

 

4.测试

源码

using System;

namespace RunDebugConfigTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            const string a = "77";
            if (a.Equals("333")) {
                Console.WriteLine("a是什么" + a);
            }else if (a.Equals("77")) {
                Console.WriteLine("777");
            }else {
                Console.WriteLine("44444444444444444444");
            }
            Console.WriteLine("==================================");
            const int b = 3;
            Console.WriteLine(b);
            
            const float c = 4.56f;
            Console.WriteLine(c);
            const double d = 5.67889;
            Console.WriteLine(d);
            const char f = 'f';
            Console.WriteLine(f);
            const bool g = true;
            Console.WriteLine(g);
            const short h = 343;
            Console.WriteLine(h);
            const long i = 123456789;
            Console.WriteLine(i);
            byte[] j = System.Text.Encoding.Default.GetBytes ( "33333333333" );
            Console.WriteLine(System.Text.Encoding.Default.GetString ( j ));
        }
    }
}
View Code

控制台打印:

 

posted @ 2020-08-27 14:16  岑惜  阅读(1856)  评论(0)    收藏  举报