4e1548abe2
Part1
35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using System;
|
|
namespace Lesson5
|
|
{
|
|
class program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
#region 常量
|
|
//常量是指在程序运行过程中不会改变的值,必须初始化
|
|
//常量的定义:const 数据类型 常量名 = 值;
|
|
//常量名一般使用大写字母,多个单词之间用下划线分隔
|
|
const int MAX_VALUE = 100;
|
|
const string COMPANY_NAME = "ZJ Company";
|
|
Console.WriteLine($"最大值: {MAX_VALUE}, 公司名称: {COMPANY_NAME}");
|
|
#endregion
|
|
#region 只读变量
|
|
//只读变量在定义时可以赋值,也可以在构造函数中赋值,但不能在其他地方修改
|
|
class MyClass
|
|
{
|
|
public readonly int ReadOnlyValue;
|
|
public MyClass(int value)
|
|
{
|
|
ReadOnlyValue = value;
|
|
}
|
|
}
|
|
MyClass myObject = new MyClass(42);
|
|
Console.WriteLine($"只读变量值: {myObject.ReadOnlyValue}");
|
|
//关键词 const
|
|
//固定写法:
|
|
//const 变量类型 变量名 = 值;
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
} |