案例1:常量 const
class Program
{
static void Main(string[] args)
{
//在变量前加入const 关键字意味着他就是一个常量
//如果我们在编写程序的时候希望一个变量不能被改变,我们就定义它为常量
//常量的标识符通常全部都是大写,程序员看到全部是大写的标识符就能明白它是一个常量
const double PI= 3.14;
//一旦定义为常量意味着它的值就不能改变
//PI = 3.24;
Console.ReadKey();
}
}
————————————————————————————————————————————————————
案例2.1 枚举
namespace e2
{ //枚举的定义方式
//访问修饰符 enum 枚举名
//{
//枚举1
//枚举2
//..........
//}
//枚举类型的作用就是限定取值范围,比如说性别男和女
//枚举不能包含方法
public enum Genders
{
Male,
Female
}
class Student
{
private Genders gender;
public Genders Gender
{
set { gender = value; }
get { return gender; }
}
public void Display()
{
Console.Write(gender);
}
}
}
——————————————————————————————————————————————————————————
案例2.2
namespace e2
{
class Program
{
static void Main(string[] args)
{
Student stu= new Student();
stu.Gender =Genders.Male;
stu.Display();
Console.ReadKey();
}
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————
案例3 枚举转换
namespace e3
{
public enum NIIT
{
MMS1=3,
MMS2=4,
MMS3=5,
MMS4=6
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0}", (int)NIIT.MMS1);//索引默认为0,也可自定义!
Console.WriteLine("枚举转字符串:{0}",NIIT.MMS3.ToString());
NIIT m3 = (NIIT)Enum.Parse(typeof(NIIT), "MMS3");
Console.WriteLine("字符串常量转枚举:{0}",m3);
Console.ReadKey();
}
}
}
————————————————————————————————————————————————————————
案例4 枚举就是自定义数据类型
namespace e4
{
public enum TIME //枚举就是自定义的数据类型!!!
{
time1 = 1,
time2 = 2,
time3 = 3,
time4 = 4,
time5 = 5,
time6 = 6,
time7 = 7,
}
class Program
{
static void Main(string[] args)
{
Console.Write("请输入:(1-7)");
int a=Convert.ToInt32(Console.ReadLine());
TIME time = (TIME)a; //枚举就是自定义的数据类型!!!
switch (time)
{
case TIME.time1:
case TIME.time2:
case TIME.time3:
case TIME.time4:
case TIME.time5:
Console.WriteLine("需要工作,泪奔......."); break;
case TIME.time6:
case TIME.time7:
Console.WriteLine("我喜欢周末!!!"); break;
default: Console.WriteLine("输入错误");break;
}
Console.ReadKey();
}
}
——————————————————————————————————————————————————————————
案例5结构
namespace e5
{
public struct StudentStruct
{
//结构 中不允许赋初始值给他的成员
public string name;
public int age;
public string hobby;
public int populraity;
//结构不允许有默认函数!
//public StudentStruct()
//{
//}
//结构里还可以包含函数
public StudentStruct(string name, int age, string hobby, int populraity)
{
this.name=name;
this.age=age;
this.hobby = hobby;
this.populraity = populraity;
}
public void SayHi()
{
Console.WriteLine("姓名:{0},年龄:{1},爱好:{2},受欢迎度:{3}", name, age, hobby, populraity);
}
}
class Program
{
static void Main(string[] args)
{
//不用new方式创建!
StudentStruct ss1;
ss1.name = "张俊杰";
ss1.age = 18;
ss1.hobby = "唱唱歌";
ss1.populraity = 110;
ss1.SayHi();
StudentStruct ss2 = new StudentStruct("周杰杰", 25, "打打球", 80);
ss2.SayHi();
Console.ReadKey();
}
}
}
————————————————————————————————————————————————————
案例6.1 值类型和引用类型
namespace e6
{
struct StudentStruct
{
public string Name;
public int Age;
public string hobby;
}
}
————————————————————————————————————————————————————
案例6.2
namespace e6
{
class Student
{
public string Name;
public int Age;
public string hobby;
}
}
————————————————————————————————————————————————
案例6.3
namespace e6
{
class Program
{
static void Main(string[] args)
{
//值类型
/*结构(struct)是值类型,所以它会以传值的方法进行传递
* 以下例子,把ss1对象赋值给ss2对象,实际上是把结构ss1对象成员如:age
* 将它的值复制了一份给ss2对象的age,相互之间无任何关系,所以即使改变了
* ss2对象的值,ss1对象的值也不会发生改变
* */
StudentStruct ss1 = new StudentStruct();
ss1.Age=18;
StudentStruct ss2=ss1;
ss2.Age=20;
Console.WriteLine("ss1结构对象的age:{0},ss2结构对象的age:{1}", ss1.Age, ss2.Age);
Console.WriteLine();
//----------------------------------------------------------------------------------------------------
//类 (class)是引用类型,它将会以传引用的方式进行传递
//以下例子将stu1对象赋给了stu2,实际上传递的是一个引用,即:
//stu1.age 和stu2.age引用的是同一个内存空间的值,所以
//stu2.age的值发生改变,两个对象的age都发生了改变
Student stu1 = new Student();
stu1.Age = 18;
Student stu2 = stu1;
stu2.Age = 20;
Console.WriteLine("类");
Console.WriteLine("stu1结构对象的age:{0},stu2结构对象的age:{1}", stu1.Age, stu2.Age);
Console.ReadKey();
}
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————
案例7 判断 早上好 下午好
namespace l1
{
public enum TimeOfDay
{
morning=1,
afternoon,
evening
}
class Program
{
static void Main(string[] args)
{
Console.Write("请输入1. morning, 2.afternoon,3.evening");
int a = int.Parse(Console.ReadLine());
TimeOfDay time = (TimeOfDay)a;
switch (time)
{
case TimeOfDay.afternoon: Console.WriteLine("Good Afternoon!"); break;
case TimeOfDay.evening: Console.WriteLine("Good Evening!"); break;
case TimeOfDay.morning: Console.WriteLine("Good Morning!"); break;
default: Console.WriteLine("输入错误....."); break;
}
Console.ReadKey();
}
}
}
————————————————————————————————————————————————————
案例8结构例子
namespace l2
{
public struct TeacherStruct
{
public string teacherName;
public int teacherAge;
public int yearsOfService;
//public TeacherStruct(string teacherName, int teacherAge, int yearsOfService)
//{
// this.teacherName = teacherName;
// this.teacherAge = teacherAge;
// this.yearsOfService = yearsOfService;
//}
public void SayHi()
{
Console.WriteLine("大家好,我是{0}老师。我已经在教育战线奋斗了{1}年了", teacherName, yearsOfService);
}
}
class Program
{
static void Main(string[] args)
{
TeacherStruct ts;
ts.teacherName = "张鬼";
ts.teacherAge = 35;
ts.yearsOfService = 15;
ts.SayHi();
Console.ReadKey();
}
}
}