冒泡算法
int[] a = { 5, 1, 7, 2, 3 };
for(int i = 0; i < a.Length; i++)
{
for(int j = 0; j < a.Length - i - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
------------------------------
枚举类型
访问修饰符 enum 变量名 : 数据类型
{
值l,
值2,
}
class EnumTest
{
public enum Title : int
{
助教,
讲师,
副教授,
教授
}
}
获取并输出枚举值,在 Main 方法中调用的代码如下。
static void Main(string[] args)
{
Console.WriteLine(EnumTest.Title.助教 + ":" + (int)EnumTest.Title.助教);
Console.WriteLine(EnumTest.Title.讲师 + ":" + (int)EnumTest.Title.讲师);
Console.WriteLine(EnumTest.Title.副教授 + ":" + (int)EnumTest.Title.副教授);
Console.WriteLine(EnumTest.Title.教授 + ":" + (int)EnumTest.Title.教授);
}
每个枚举值的整数值都是前一个枚举值的整数值加 1
◆ 结构体
在结构体中能定义字段、属性、方法等成员。定义的语法形式如下。
访问修饰符 struct 结构体名称
{
//结构体成员
}
public struct student
{
public student(string name, int age)
{
this.name = name;
this.age = age;
}
private string name;
private int age;
public void PrintStudent()
{
Console.WriteLine("姓名:" + name);
Console.WriteLine("年龄:" + age);
}
}
static void Main(string[] args)
{
student stu = new student();
stu.Name = "张三";
stu.Age = -100;
Console.WriteLine("学生的信息为:");
Console.WriteLine(stu.Name + ":" + stu.Age);
student stu = new student("李四",25);
stu.PrintStudent();
}
结构体 | 类 |
---|---|
允许不使用new对其实例化 | 必须使用new实例化 |
没有默认构造方法 | 有默认构造方法 |
不能继承类 | 能继承类 |
没有析构方法 | 有析构方法 |
不允许使用abstract、protected以及sealed修饰 | 允许使用abstract、protected以及sealed修饰 |