5.1 枚举
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 枚举
{
class Program
{
//5.1枚举
//C#支持枚举类型,简单地说就是把字符串映射到数字0、1、2...上的类型
//需要注意的是,枚举类型的定义代码要卸载主函数的外面,和主函数并列放置
enum WeekDays { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
static void Main(string[] args)
{
WeekDays today = WeekDays.Sunday;
if ((today == WeekDays.Saturday) || (today == WeekDays.Sunday))
{
Console.WriteLine("I love the weekends!");
}
else
{
Console.WriteLine("Back to work.");
}
//表面上7个枚举项都是字符串,实际上每个枚举项都关联了一个整数,
//所以我们可以直接用枚举项关联的整数进行赋值,但是必须进行显式转换(强制类型转换)
today = (WeekDays)3;
Console.WriteLine("Today is " + today); //运行结果Today is Wednesday
//如果我们想输出该枚举项关联的整数,语句如下
today = WeekDays.Sunday;
Console.WriteLine((int)today);
//因为每个枚举项都关联一个整数,所以枚举型变量可以参与运算和循环
for(today = WeekDays.Sunday; today<=WeekDays.Saturday; today++)
{
Console.WriteLine(today);
}
//在默认情况下枚举项的关联值一次对应0、1、2、3...。
//必要时我们可以通过赋值的方法改变枚举项所关联的整数值
/*
enum WeekDays
{
Sunday = 5;
Monday,
Tuesday,
Wednesday,
Thursday = 100;
Friday;
Saturday;
}
Console.WriteLine(" Sunday: {0}", (int)WeekDays.Sunday);
Console.WriteLine(" Monday: {0}", (int)WeekDays.Monday);
Console.WriteLine(" Tuesday: {0}", (int)WeekDays.Tuesday);
Console.WriteLine("Wednesday: {0}", (int)WeekDays.Wednesday);
Console.WriteLine(" Thursday: {0}", (int)WeekDays.Thursday);
Console.WriteLine(" Friday: {0}", (int)WeekDays.Friday);
Console.WriteLine(" Saturday: {0}", (int)WeekDays.Saturday);
*/
//运行结果如下
/*
Sunday: 5
Monday: 6
Tuesday: 7
Wednesday: 8
Thursday: 100
Friday: 101
Saturday: 102
*/
//在默认情况下,枚举项关联值的类型为int型,如果需要,
//我们可以为枚举项指定更合适的数据类型。关联值可以为byte、
//sbyte、short、ushort、int、uint、long、ulong等类型。
/*
enum WeekDays: short
{
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
*/
}
}
}
运行结构如下:
5.2 结构体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 结构体
{
//和枚举类型一样,结构体的定义代码应在主函数之外。其实所有定义类型的代码都应在主函数之外
//定义结构体
struct Student
{
public int number;
public string name;
public string sex;
public string birthday;
}
class Program
{
static void Main(string[] args)
{
//声明变量hfy并为其成员变量赋值
Student hfy;
hfy.number = 2000069001;
hfy.name = "寒飞雨";
hfy.sex = "男";
hfy.birthday = "1980.11.06";
//声明变量honey并为其成员变量赋值
Student honey;
honey.number = 2000070040;
honey.name = "Britney jean Spears";
honey.sex = "女";
honey.birthday = "1981.12.02";
//输出honey的信息
Console.WriteLine("学号:" + hfy.number);
Console.WriteLine("姓名:" + hfy.name);
Console.WriteLine("性别:" + hfy.sex);
Console.WriteLine("生日:" + hfy.birthday);
//输出honey的信息
Console.WriteLine("学号:" + honey.number);
Console.WriteLine("姓名:" + honey.name);
Console.WriteLine("性别:" + honey.sex);
Console.WriteLine("生日:" + honey.birthday);
}
}
}
运行结构如下:
5.3 数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 数组
{
class Program
{
static void Main(string[] args)
{
//声明数组
int[] scores = {120,149,112,133,127};
//用for语句处理数组
int sum = 0;
for(int i = 0;i<scores.Length;i++)
{
Console.WriteLine(scores[i]); //输出5科成绩
sum = sum + scores[i]; //计算5科总分
}
float average = sum / (scores.Length); //计算5科平均成绩
Console.WriteLine("总 分:{0}",sum);
Console.WriteLine("平均值:{0}", average);
string[] friendNames = new string[3];
friendNames[0] = "张三";
friendNames[1] = "李四";
friendNames[2] = "王五";
Console.WriteLine("我共有{0}位朋友:",friendNames.Length);
for(int i=0; i<friendNames.Length; ++i)
{
Console.WriteLine(friendNames[i]);
}
//forreach循环语句
string[] friends = {"张三","李四","王五"};
Console.WriteLine("我共有{0}位朋友:", friends.Length);
foreach (string mingzi in friends)
{
Console.WriteLine(mingzi);
}
//二位数组
//声明二维数组
int[,] Matrix = { {1,2,3},{4,5,6},{7,8,9} };
//双重for语句遍历
for(int i = 0; i<3; ++i)
{
for(int j = 0; j<3; ++j)
{
Console.Write(Matrix[i,j] + " ");
sum += Matrix[i,j];
}
Console.WriteLine("\n"); //每输出三个数字换行
}
//不等长二维数组
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[3] { 1, 2, 3 };
jaggedArray[1] = new int[4] { 1, 2, 3 ,4};
}
}
}
运行结果如下: