C#学习之路,学习笔记 第五章 枚举、结构体和数组
来源:互联网 发布:程序员用的字体 编辑:程序博客网 时间:2024/05/05 00:09
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}; } }}运行结果如下:
0 0
- C#学习之路,学习笔记 第五章 枚举、结构体和数组
- C#学习笔记之七:结构和枚举
- C#学习笔记001-结构和枚举
- C#学习笔记之——枚举、结构体
- 黑马程序员-学习枚举、结构体和数组的笔记
- c语言学习笔记之结构体和枚举
- c#学习笔记--数组、枚举、结构、值类型和引用类型
- 学习笔记之C#常量、枚举、结构、索引器
- Swift学习之路 -- 枚举和结构体
- C#学习笔记6——C#中枚举、结构、数组
- C#基础之结构体&枚举&数组
- c#学习笔记之五 C与C#在几种主要数据结构在上区别 数组和枚举
- c#学习第五天 枚举和结构体
- C++学习笔记之结构、枚举
- <C#入门经典>学习笔记之结构、数组及字符串
- Swift学习笔记 -- 枚举和结构
- c语言学习笔记之结构体,枚举
- 黑马程序员_C语言学习笔记之结构体和枚举
- struts <html:form>: Module 'null' not found
- Wormholes
- 【Visual C++】游戏开发笔记三十七 浅墨DirectX提高班之五 顶点缓存的红颜知己:索引缓存的故事
- Intent的两种具体实现
- PIC单片机延时问题
- C#学习之路,学习笔记 第五章 枚举、结构体和数组
- D3DPRESENT_PARAMETERS
- HDOJ 题目4568 Hunter(BFS+状压DP,TSP)
- leetcode之Rotate Image
- 【电子设计大赛】PID 控制算法整理
- HDU 5478 Can you find it
- Effective Objective-C 2.0(上)
- Quartus 11.0 Internal Error veriname_elab.cpp
- D3D9学习笔记(三) Device