你的位置:首页 > ASP.net教程

[ASP.net教程]【C#学习笔记】一、基础知识

1.1数据类型(P43)

类型

别名

允许的值

sbyte

System.SByte

-128~127

byte

System.Byte

0~255

short

System.Int16

-32768~32767

ushort

System.Uint16

0~65535

int

System.Int32

-2147483648~2147483647

uint

System.UInt32

0~4294967295

long

System.Int64

-9223372036854775808~9223372036854775807

ulong

System.UInt64

0~18446744073709551615

char

System.Char

一个Unicode字符,0~65535

bool

System.Boolean

true/false

sring

System.String

一组字符

float

System.Single

1.5*10^-45~3.4*10^38

double

System.Double

5.0*10^-324~1.7*10^308

decimal

System.Decimal

1.0*10^-28~7.9*10^28

1.2枚举类型(P97)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//【例子1】枚举的基本应用

//1)赋值是可以使用其枚举值赋值,如ct=emCardType.Temic;也可以直接用数值赋值,如ct = (emCardType)3;也可用将字符串经过转换//  赋值给枚举变量,如ct = (emCardType)Enum.Parse(typeof(emCardType), "M1")

//2)使用WritenLine时其实是调用其ToString()方法将其转换为字符串

//3)可以将没有枚举到的数值赋给枚举变量,但ToString()方法显示的字符串为其值

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

enum emCardType:byte

{

    Temic,

    Em=2,

    ID,

    M1

}

emCardType ct;

ct=emCardType.Temic ;

Console.WriteLine("ct is {0},value is {1}", ct,(byte)ct);             //显示"ct is Temic,value is 0"

ct++;

Console.WriteLine("ct is {0},value is {1}", ct,(byte)ct);             //显示"ct is 1,value is 1"  

ct = (emCardType)Enum.Parse(typeof(emCardType), "M1");

Console.WriteLine("ct is {0},value is {1}", ct, (byte)ct);            //显示"ct is M1,value is 4"

ct = (emCardType)3;

Console.WriteLine("ct is {0},value is {1}", ct.ToString(), (byte)ct); //显示"ct is ID,value is 3"

Console.ReadKey();

1.3数组

1)一维数组定义

int[] myIntArray = { 5, 9, 10, 2, 99 };

int[] myIntArray = new int[5];

int[] myIntArray = new int[5] { 5, 9, 10, 2, 99 };

2)多维数组定义

double[,] hillHeight = new double[3, 4];

double[,] hillHeight = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };

3)数组的数组

int [][] myarray

myarray=new int[2][];

myarray[0]=new int[3];

myarray[1]=new int[4];

 

myarray=new int[3][](new int[]{1,2,3},new int[]{1},new int[]{1,2});

foreach (int[] subarray in myarray)

{

foreach(int nValue in subarray)

        Console.WriteLine(nValue);

}

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//【例子1】数组复制

//若直接使用=号则两个数组引用相同的值,要使用CopyTo方法进行复制

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int[] myInt = new int[5]{5,2,1,8,4};

foreach (int i in myInt)

Console.Write(i + ",");

Console.WriteLine("");

int[] myInt2=new int[5];

myInt.CopyTo(myInt2,0);

//myInt2 = myInt;

myInt[1] = 3;

foreach (int i in myInt2)

    Console.Write(i + ",");

1.4委托(P137)

【例子1

class Program

{

delegate double ProcessDelegate(double param1,double param2);

static double Multiply(double param1,double param2)

{

        return param1*param2;

}

static double Divide(double param1,double param2)

{

        return param1/param2;

}

static void Main(string[] args)

{

        ProcessDelegate process;

        if (input=="M")    //根据输入的不同将其实例化为不同的函数

            process=new processDelegate(Multiply);

        else

            process=new processDelegate(Divide);

        double fRet=process(f1,f2);

}

}

 

【例子2

private delegate int mif_selecom(int nCom, int nBaud);

private void button1_Click(object sender, EventArgs e)

{

     IntPtr pDll = LoadLibrary("LC32RFRW.dll");

     IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "mif_selecom");

     mif_selecom DoSeleCom = (mif_selecom)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall,

typeof(mif_selecom));

}