手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

Visual Studio:针对 Java 开发人员的 C# 编程语言

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

运行此程序,输出如下:

Integer parameter

Float parameter

String parameter

sizeof 运算符

sizeof 运算符以指定值类型的字节数返回其大小,如下面的代码所示:

using System;

public class Size

{

  public static void Main()

  {

    unsafe

    {

      Console.WriteLine("The size of short is {0}.", sizeof(short));

      Console.WriteLine("The size of int is {0}.", sizeof(int));

      Console.WriteLine("The size of double is {0}.",sizeof(double));

    }

  }

}

注意,包含 sizeof 运算符的代码放在一个不安全的块中。这是因为 sizeof 运算符被认为是一个不安全的运算符(由于它直接访问内存)。有关不安全代码的更多信息,请参见安全代码和不安全代码。

typeof 和 GetType

typeof 运算符返回作为 System.Type 对象传递给它的类的类型。GetType() 方法是相关的,并且返回类或异常的运行时类型。typeof 和 GetType() 都可以与反射一起使用,以动态地查找关于对象的信息,如下面的示例所示:

using System;

using System.Reflection;



public class Customer

{

    string name;



    public string Name

    {

      set

      {

        name = value;

      }

          

      get

      {

        return name;

      }

    }

}



public class TypeTest

{

    public static void Main()

    {

      Type typeObj = typeof(Customer);



      Console.WriteLine("The Class name is {0}",

         typeObj.FullName);

      

     // Or use the GetType() method:

      //Customer obj = new Customer();

      //Type typeObj = obj.GetType();



      Console.WriteLine("\nThe Class Members\n=================\n ");

      MemberInfo[] class_members = typeObj.GetMembers();

      foreach (MemberInfo members in class_members)

      {

        Console.WriteLine(members.ToString());

      }



      Console.WriteLine("\nThe Class Methods\n=================\n");

      MethodInfo[] class_methods = typeObj.GetMethods();

      foreach (MethodInfo methods in class_methods)

      {

        Console.WriteLine(methods.ToString());

      }

    }

}

运行此程序,输出如下:

The Class name is Customer



The Class Members

=================



Int32 GetHashCode()

Boolean Equals(System.Object)

System.String ToString()

Void set_Name(System.String)

System.String get_Name()

System.Type GetType()

Void .ctor()

System.String Name



The Class Methods

=================



Int32 GetHashCode()

Boolean Equals(System.Object)

System.String ToString()

Void set_Name(System.String)

System.String get_Name()

System.Type GetType()

这为我们显示了从 System.Object 继承的所有类的成员,并且还展示了一种方法,C# 在内部将 get 和 set 属性 Accessors 表示为 get_xxx() 和 set_xxx() 方法。

在下一个示例中,我们使用 GetType() 在运行时查找表达式的类型:

using System;

public class TypeTest

{

  public static void Main()

  {

    int radius = 8;

    Console.WriteLine("Calculated area is = {0}",

                             radius * radius * System.Math.PI);

    Console.WriteLine("The result is of type {0}",

                             (radius * radius * System.Math.PI).GetType());

  }

}

此程序的输出告诉我们,结果是 System.Double 类型,选择它是因为System.Math.PI 是这种类型。

Calculated area is = 201.061929829747

The result is of type System.Double

返回页首

流程控制

在这两种语言中,流程控制语句是非常相似的,但是这一部分也会讨论它们的一些细微差别。

分支语句

分支语句根据特定的条件改变运行时程序执行的流程。

if、else 和 else if

这些在两种语言中是一样的。

switch 语句

在两种语言中,switch 语句都提供条件多分支操作。但是有点不同的是,Java 允许您“越过”一个 case 并执行下一个 case,除非您在 case 的末尾使用了 break 语句。然而,C# 需要在每个 case 的末尾都使用 break 或 goto 语句,如果两者都不存在,则编译器会产生下列错误:

Control cannot fall through from one case label to another.

不过请注意,在没有指定要执行的代码的地方,当 case 匹配时,控制会越过随后的 case。当在 switch 语句中使用 goto 时,我们只能跳至同一 switch 中的另一个 case 块。如果我们想要跳至 default case,我们可以使用“goto default;”,否则,我们需要使用“goto case cond;”,其中 cond 是我们希望跳至的 case 的匹配条件。Java 的 switch 语句的另一个不同之处在于,在 Java 中,我们只能对整数类型使用 switch 语句,而 C# 允许我们对字符串变量使用 switch 语句。

例如,下面的程序在 C# 中是合法的,但在 Java 中却是不合法的:

switch (args[0])

{

  case "copy":

    ...

    break;

  case "move":

    ...

    goto case "delete";

    break;

  case "del":

  case "remove":

  case "delete":

    ...

    break;

  default:

    ...

    break;

}

goto 的返回

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!