1、常量:访问修饰符 const 数据类型 常量名=值
使用场合:A、用于在程序中一旦设定就不允许被修改的值
B、用于在程序中被经常引用的值
2、枚举:表示一组有限的值,对可能的值进行约束。为了防止用户乱输入,规范用户的输入。
2.1、给枚举成员赋值只能赋整型,不能赋字符串。
2.2、转换:A、枚举转换为整型 如int genderNum=(int)stu.Gender
B、枚举转换为字符串 如Console.WriteLine("您输入的性别是{0}",stu.Gender.ToString())
C、从一个字符串中获取枚举的值
如stu.Gender=(Genders)(Enum.Pase(typeof(Genders),"Female"))
示例:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace MySchool
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public enum Genders
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Male = 0, Female = 1
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class Student
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Genders gender;
public Genders Gender
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return gender; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ gender = value; }
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace MySchool
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Student stu = new Student();
stu.Gender = Genders.Male;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int genderNum = (int)stu.Gender;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch (genderNum)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 0:
Console.WriteLine("您输入的是性别是-男");
break;
case 1:
Console.WriteLine("您输入的是性别是-女");
break;
}
Console.WriteLine("您输入的是性别是{0}", stu.Gender.ToString());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
stu.Gender = (Genders)(Enum.Parse(typeof(Genders), "Female"));
Console.WriteLine("您输入的是性别是{0}", stu.Gender.ToString());
}
}
}
注意:在枚举的主体中,是不可以定义属性和方法
枚举与类是同一级别的,因此要单独新建
3、结构:访问修饰符 struct 结构名
{
定义结构成员
}
类与结构的区别: 类 结构
引用类型 值类型
可以被继承 不能被继承
可以有默认构造函数 不可以有默认构造函数
可以添加无参的构造函数 可以添加构造函数,但它们必须带参数
可以有实例字段初始值 不能有实例字段初始值
使用场合:结构用于组织一堆数据。
1.该类型的主要职责是否用于数据存储?
2.该类型的公有接口是否都是一些存取属性?
3.是否确信该类型永远不可能有子类?
4.是否确信该类型永远不可能具有多态行为?
4、值类型和引用类型的区别:主要是在内存中的存储方式不同造成的。
值类型将一个值类型变量赋给另一个值类型变量时,将复制包含的值(替身)。
引用类型变量的赋值只复制对象的引用,而不复制对象的本身。
两者的转换:示例
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Boxing
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Boxing
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = 123;
object o = i; // 装箱
i = 456; //改变i的内容
System.Console.WriteLine("值类型的值为 {0}", i);
System.Console.WriteLine("引用类型的值为 {0}", o);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//static void Main(string[] args)
//{
// int i = 123;
// object o = i; // 隐装箱
// try
// {
// int j = (int)o; // 拆箱
// System.Console.WriteLine("取消装箱成功.");
// }
// catch (System.InvalidCastException e)
// {
// System.Console.WriteLine("{0} 错误: 不正确的取消装箱.", e.Message);
// }
//}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
引用类型:类(基类:System.Object/字符串:string,但是表现为值类型的特征/自定义类:class)
接口:interface
数组
值类型:主要包括基本的数据类型、结构类型、枚举类型等
5、在类中使用索引器:示例
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace MySchool
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class Students
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Student[] students = new Student[3];
public Students()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
students[0] = new Student("Scofield", Genders.Male, 28, "越狱狱");
students[1] = new Student("张靓靓", Genders.Female, 20, "唱歌歌");
students[2] = new Student("周杰杰", Genders.Male, 21, "耍双节棍棍");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//基本索引器 根据数组下标查找学员
public Student this[int index]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return students[index]; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//与 Java 中的 getter 方法类比 证明方法可以返回一个类型
//public Student GetStudent(int index)
//{
// return students[index];
//}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//重载的索引器 根据姓名查找学员
public Student this[string name]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i;
bool found = false;
for (i = 0; i < students.Length; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (students[i].Name == name)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
found = true;
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (found)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return students[i];
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return null;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)