c#基本语法 一.c#中变量的定义方法
1.string aa; 分号可不能丢 c# 每行后都加上; string AA;
c# 是区分大小写的 上面两种定义是正确的。 2.string a=\ 用“ ,”分割
二.数据类型包括 值类型和引用类型
值类型就是存值 引用类型是存其对值得引用 就是变量的地址。二者的区别就是存储位置和存储的形式不同。
string 字符串 是引用类型 string i=\string c=i+j; 结果为 35
char类型 char 'c' 用单引号定义 三.日期类型
DateTime数据类型 System.DateTime 1. DateTime dt; dt=DateTime.Now;
DateTime dat=Convert.ToDateTime(\ Response.Write(“年:”+dt.Year); 年:2007 Response.Write(“月:”+dt.Month); Response.Write(“日:”+dt.Day);
Response.Write(dt.tolongDateString()); 2007年3月20号 Response.Write(dt.tolongTimeString()); 15:30:23 Response.Write(dt.toShortDateString()); 2007-3-20 Response.Write(dt.toShortTimeString()); 15:30 四.运算符
包括 算术运算符 赋值运算符 比较运算符 逻辑运算符 连接运算符 = += -= *= /= ?? %= a=value 就是把value的值赋给a a??b a非空返回a 否则返回b sting a=\
a+b 和a+=b 返回的结果是一样的 均为123fff 连接运算符 五.数据转换 -扩大转换和缩小转换 Convert int a=23;
byte b=Convert.ToByte(a); 23
byte b=43; int f=b; 43
Double r=342.723;
int i=Convert.ToInt32(r); 343 六.数组
1.string [] arr; 只是声明了数组的引用 数组实体还是不存在。
arr=new string[5]; 数组初始化 数组下标从0开始 此数组的最大下标是4 string [] thc={\thc[0]=aa 修改数组中的方法
一法 thc[0]=\
二法 for(int i=0;i thc[i]=\ } 2.二维数组 string [ , ] ab=new string [2][4]; ab[0,0]=\ab[0,1]=\ ab[0,2]=\ab[0,3]=\ab[1,0]=\ab[1,1]=\ab[1,2]=\ab[1,3]=\ for(int i=0;i for(int j=0;j Response.Write(\ } } 结果: 1 2 3 4 5 6 7 8 七.结构 struct 结构是一种值类型,可以包含不同数据类型的数据,还可以包含复合类型(数组 结构 DateTime 等) 除了以名字/值得方式出现 还可以有属性 方法 所以说结构是一种非常强大的复合型数据。例如:矩形的坐标 或库存商品的特征 Public Struct Book { Public int price; Public string title; 类型可以不同 Public string author; } 枚举类型 Enum ee { abc=0, 中间用,分割 bcd=1, cde=2 } Enum ee {thc,hxm,thw}; 枚举未赋值时从0开始自动给他们赋值。所以这两种定义是相同的 Enum ee{ t=2, 枚举也可以不从0开始赋值 c=3, a=5 } 八.循环 if....esle.... try...catch... switch...case...default.... try { } catch { } seitch() { case 1: ''''''' bresk; case 2: ''''''' break; default: '''''' break; } for循环 do...where... where...do... foreach....in循环 string[] thc=new string[] {\foreach (string r in thc) { if(r.Contains(\ { Response.write(r.Tostring()+\ } } 九.函数 1.和java一样,c#不允许有全局函数,所有的函数都必须在类或结构内实现。函数是类或结构的成员,函数也 被称为方法。 2.在构造的时候,函数的参数必须申明类型,两个参数之间用,分割 (string a,string bb) 返回类型 函数名(参数1,参数2) { } 传递参数有两种 传值和传址