您的位置:首页 > 编程语言 > C#

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();

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: