C# 代码示例_结构/数组/枚举...

时间:2023-03-09 15:45:47
C# 代码示例_结构/数组/枚举...
 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=,
south=,
east=,
west=
} //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 = ;
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 = -;
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 < || intDirection > );
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 = {,,,,};
int[] secondArray=new int[];
int[] thirdArrary = new int[] { ,,}; //the number should be the same.
const int count = ;
int[] forthArray = new int[count]; //multidimensional array
double[,] multiArray1 = new double[, ];
double[,] multiArray2 = { {,},{,},{,}}; //jagged array
int[][] jaggedArray = new int[][];
jaggedArray[]=new int[];
jaggedArray[] = new int[]; int[][] jaggedArray2 = { new int[]{,}, new int[]{}, new int[]{,}};
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(); }
}
}

相关文章