1、C#居然可以使用中文的变量名、类名、函数名以及命名空间;
2、使用@"C:\TDDOWNLOAD"这种格式的字符串,可以使转义符失去转义功能,从而让"\"原样打出;
3、C#字符串的比较使用==或!=(Java使用.equals()方法);
4、C#中的类型转换:
int r;
if (int.TryParse("123", out r))
{
Console.WriteLine(r);
}
else
{
Console.WriteLine("转换失败");
}
5、foreach:
string[] strs = {"zhangsan","lisi","wangwu"};
foreach(string str in strs)
{
Console.WriteLine(str);
}
6、params关键字:可变参数
static void Main(string[] args)
{
println("zhangsan", "lisi", "wangwu");
Console.ReadKey();
}
static void println(params string[] args)
{
foreach (string str in args)
{
Console.WriteLine(str);
}
}
7、C#的字符串变量可以直接当成数组通过下标读取出来,但无法对数组元素赋值;
8、ref关键字:设置传入引用
static void Main(string[] args)
{
int a = 1, b = 2;
//交换两个数字
swap(ref a, ref b);
Console.WriteLine("a={0},b={1}",a,b);
Console.ReadKey();
}
//ref声明的参数传入的是引用
static void swap(ref int a, ref int b)
{
int c = a;
//对引用指向的内容进行修改
a = b;
b = c;
}
9、out关键字:一般用于给多个参数赋值
static void Main(string[] args)
{
int a, b, c, d;
setValues(out a,out b,out c,out d);
Console.WriteLine("a={0},b={1},c={2},d={3}",a,b,c,d);
Console.ReadKey();
}
//out一般用于给多个参数赋值
static void setValues(out int a,out int b,out int c,out int d)
{
a = 1; b = 2; c = 3; d = 4;
}
10、C#、C++、PHP都是用const关键字定义常量;而Java是用final关键字定义常量;
11、C#中的不可空类型(int,bool,decimal,DateTime等)在类型后面加一个?就可以赋null值了;
值得注意的是,在java中,将一个Integer的值直接给一个int变量时,如果Integer的值为null,则程序会报错,因此需要自己判断是否为null;
int? i = null;
DateTime? d = null;
Integer integer = null;
int i = integer==null?0:integer;
12、int?可以向int类型强制转换,当然程序员要保证int?的变量不能为空,否则会报错
int? a = 3;
int b = (int)a;