C# 代码示例_结构/数组/枚举...
2014-02-03 10:53
399 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { //Enum Definition enum orientation : byte { north=1, south=2, east=3, west=4 } //Structure Definition struct route { public orientation direction; public double distance; } static void Main(string[] args) { #region regionTest Console.WriteLine("From the beginning again."); Console.ReadLine(); #endregion //Type Conversion /* Keyword for OverflowException check: checked, unchecked * or you can configure the application: * 1) Right click the project in Solution Explorer panel,select 'Properties' from context menu * 2) Select 'Build' in the left navigation bar, click 'Advanced' button. * 3) Tick off 'Check for arithmetic overflow/underflow' checkbox, click 'OK'. */ byte destinationVar; short sourcevar = 281; destinationVar = checked((byte)sourcevar); //will popup an error when running Console.WriteLine("destinationVar val:{0}", destinationVar); //Enum practice byte directionByte; string directionString; orientation myDirection = orientation.north; Console.WriteLine("myDirection = {0}",myDirection); directionByte = (byte)myDirection; //must use explicit conversion even though it's of byte. //enum to string directionString = Convert.ToString(myDirection); directionString = myDirection.ToString(); // same rsult as the one above Console.WriteLine("byte equivalent = {0}",directionByte); Console.WriteLine("string equivalent={0}",directionString); //string to enum string myString = "east"; orientation yourDirection = (orientation)Enum.Parse(typeof(orientation), myString); ////Struct practice route myRoute; int intDirection = -1; double doubleDistence; Console.WriteLine("1) North\t2) South\t3) East\t4) West"); do { Console.WriteLine("Select a direction:"); intDirection = Convert.ToInt32(Console.ReadLine()); } while (intDirection < 1 || intDirection > 4); Console.WriteLine("Input a distance:"); doubleDistence = Convert.ToDouble(Console.ReadLine()); myRoute.direction = (orientation)intDirection; myRoute.distance = doubleDistence; Console.WriteLine("myRoute specifies a direction of {0} and a distance of {1}.", myRoute.direction, myRoute.distance); //Array Definition int[] firstArrary = {1,4,43,68,18}; int[] secondArray=new int[5]; int[] thirdArrary = new int[3] { 1,4,6}; //the number should be the same. const int count = 2; int[] forthArray = new int[count]; //multidimensional array double[,] multiArray1 = new double[3, 4]; double[,] multiArray2 = { {1,6},{6,90},{9,2}}; //jagged array int[][] jaggedArray = new int[2][]; jaggedArray[0]=new int[4]; jaggedArray[1] = new int[2]; int[][] jaggedArray2 = { new int[]{2,7}, new int[]{6}, new int[]{2,10}}; foreach (int[] divisorsOfInt in jaggedArray2) { foreach (int divisor in divisorsOfInt) { Console.WriteLine(divisor); } } //String Manipulation string mystring = "Happy New Year"; char[] mychars = mystring.ToCharArray(); foreach (char character in mystring) { Console.WriteLine("{0}", character); } Console.ReadLine(); } } }
相关文章推荐
- c#之旅--第四天(结构,枚举,数组)
- C#中遍历枚举示例代码
- C#枚举、结构、数组、排序
- 【1】C#基础:数据类型、运算、随机、枚举、结构、数组、列表、文件和文件流、序列化、MD5
- C#学习第四天 常量、枚举、结构、数组
- C#基础-结构、枚举和数组(Day4)
- 温故而知新(2)——C#变量(枚举,结构,数组)
- c#学习笔记--数组、枚举、结构、值类型和引用类型
- C#枚举类型、枚举类型与其他类型转换、结构类型、数组类型
- C#入门--其他变量(枚举、结构、数组)
- C#变量(枚举,结构,数组)
- C#学习笔记12:枚举、结构、数组基础学习
- C#中的数组,结构,枚举
- C#数组 枚举 结构
- c#第5章 变量的更多内容 隐式和显式转换、枚举、结构、数组、
- C# 枚举、结构和数组
- C#学习笔记6——C#中枚举、结构、数组
- Enums and Structs in C#(C#里的枚举和结构) (from codeproject)
- C#通过接口与线程通信(捕获线程状态)示例代码
- C#版异步TCP聊天服务器端简单代码示例