C# 与 Java 的不同点总结

本文对比了C#与Java的一些不同之处,详细介绍了C#特有的语法特性,包括@字符串、可空类型、sealed关键字、部分类、只读字段、静态构造函数等。

关于:C#Java 的不同点总结(C#的特点):

1.@

在字符窜前面加上@可使字符窜中的转义符失效。可尝试实行如下代码:

  1. string aaa = @"C:\\tmp\\aaa.txt";
  2. Console.WriteLine(aaa);
  3. aaa = "C:\\tmp\\aaa.txt";
  4. Console.WriteLine(aaa);

2.?可空类型和运算符

定义数据类型时可使用?以表示该数据库性允许为null。例如:

  1. int? a = null;
  2. Console.WriteLine(a);

3.is运算符

判断类型是否兼容,相当于java的instanceof关键字。

 

4.sealed关键字

相当于java的final,表示类或方法不可继承(密封)。

 

5.virtual关键字

表示该方法或该属性能够被重写。在java中默认情况下所有方法和属性都能被重写。

 

6.部分类关键字partial

允许类,结构或接口放入多个文件中。

  1.     //TheBigClass1.cs
  2.     partial class TheBigClass 
  3.     {
  4.         public void MethodOne()
  5.         { 
  6.         }
  7.     }
  8.     //TheBigClass2.cs
  9.     partial class TheBigClass
  10.     {
  11.         public void MethodTwo()
  12.         {
  13.         }
  14.     }

7.结构:struct

  1.     struct Dimensions 
  2.     {
  3.         public double Length;
  4.         public double Width;
  5.     }

8.只读字段readonly。

和定义常量字段的coust的区别是,readonly字段允许在类的构造函数中进行初始化(静态构造函数中也可以初始化)。

 

9.静态构造函数

  1.     class MyClass 
  2.     {
  3.         public static string value;
  4.         //静态构造函数
  5.         static MyClass()
  6.         { 
  7.             //首次加载类时运行,只执行一次
  8.             value = "A";
  9.         }
  10.         //实例构造函数
  11.         private MyClass() 
  12.         { 
  13.             //每次创建类的实例时执行
  14.             value = "B";
  15.         }
  16.         static void Main(string[] args)
  17.         {
  18.             Console.WriteLine(value);//A
  19.             MyClass clz = new MyClass();
  20.             Console.WriteLine(value);//B
  21.         }
  22.     }

10.out关键字

允许函数从一个例程中输出多个值。

请注意,使用out关键字时,变量是通过引用传值的。所以在从被调用的方法返回时,方法对该变量的任何修改都会被保留下来。

  1.         static void Main(string[] args)
  2.         {
  3.             int i;
  4.             initInt(out i);
  5.             Console.WriteLine(i);
  6.         }
  7.         static void initInt(out int i) {
  8.             i = 100;
  9.         }

11.ref参数实现

给方法传递参数时的引用传值。(通过值传递变量是默认的)

  1.         static void initInts(int[] ints, ref int i)
  2.         {
  3.             ints[0] = 200;
  4.             i = 300;
  5.         }

在调用方法时对应的字段也要加上ref关键字

initInts(ints, ref i)

注意:使用引用传值,该方法对变量的任何改变都会影响原来对象的值。

 

12.as运算符

判断类型是否兼容,不兼容返回null.

  1.     class MyClass 
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             object o1 = "Some String";
  6.             object o2 = 5;
  7.             string s1 = o1 as string;
  8.             string s2 = o2 as string;
  9.             Console.WriteLine(s1);//"Some String"
  10.             Console.WriteLine(s2);//null
  11.         }
  12.     }

13.sizeof运算符

确定栈中值类型需要的长度(单位:字节)

  1.     class MyClass
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Console.WriteLine(sizeof(int));
  6.         }
  7.     }

14.typeof运算符

返回一个特定类型的System.Type对象。

 

15.空接合运算符??

  1.     class MyClass
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             int? a = null;
  6.             int b;
  7.             b = a ?? 10;
  8.             Console.WriteLine(b);//10
  9.             a = 3;
  10.             b = a ?? 10;
  11.             Console.WriteLine(b);//3
  12.         }
  13.     }

如果第一个操作数不是null,则整个表达式等于第一个操作数的值。如果第一个操作数等于null,则这个表达式等于第二个操作数。

如果第二个操作数不能隐含的转换为第一个操作数的的类型,就生成一个编译错误。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-464492/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-464492/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值