C# 中的变量和常量
1、 声明变量
2、 变量初始化
3、 使用变量
using System;
namespace Lesson04{
class MainClass{
public static void Main (string[] args){
// 声明变量;声明定义一个整型变量
int level;
// 对变量进行初始化
level = 6;
// 值发生变化(变量是可变的)
level = 7;
// 使用变量
Console.WriteLine (level);
// 声明一个float 类型的变量exp
float exp;
// 初始化变量
exp = 0.6f;
Console.WriteLine (exp);
// 声明一个string类型的变量str
string str;
// 初始化变量
str = "你好老王";
Console.WriteLine (str);
// 声明并初始化整形变量
int a = 9;
// 值发生变化 a = 10
a = 10;
Console.WriteLine (a);
// 字面值常量不能被赋值(比如以上6;0.6f;"你好老王";都称为字面值常量)
// 声明并初始化整形变量
int b = 9;
// 值发生变化 这时b的值就不会是9 而会是10
b = 10;
Console.WriteLine (b);
// 声明并初始化整形变量a
// const常量 定义后值不能改变
const int c = 9;
Console.WriteLine (c);
// 圆周率用常量表示
const float pi = 3.1415926f;
Console.WriteLine (pi);
}
}
}