<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. C語言

    運算符關鍵字typeof的使用

    時間:2024-12-27 14:37:43 C語言 我要投稿
    • 相關推薦

    運算符關鍵字typeof的使用

      引導語:C語言是一種計算機程序設計語言,它既具有 高級語言的特點,又具有 匯編語言的特點。以下是小編整理的運算符關鍵字typeof的使用,歡迎參考閱讀!

      用于獲取類型的 System.Type 對象。typeof 表達式采用以下形式:

      System.Type type = typeof(int);

      備注

      若要獲取表達式的運行時類型,可以使用 .NET Framework 方法 GetType,如以下示例中所示:

      int i = 0;

      System.Type type = i.GetType();

      不能重載 typeof 運算符。

      typeof 運算符也能用于公開的泛型類型。具有不止一個類型參數的類型的規范中必須有適當數量的逗號。下面的示例演示如何確定方法的返回類型是否是泛型 IEnumerable<(Of <(t>)>)。假定此方法是 MethodInfo 類型的實例:

      string s = method.ReturnType.GetInterface

      (typeof(System.Collections.Generic.IEnumerable<>).FullName

      示例

      C#

      public class SampleClass2

      {

      public int sampleMember;

      public void SampleMethod() {}

      static void Main()

      {

      Type t = typeof(SampleClass);

      // Alternatively, you could use

      // SampleClass obj = new SampleClass();

      // Type t = obj.GetType();

      Console.WriteLine("Methods:");

      System.Reflection.MethodInfo[] methodInfo = t.GetMethods();

      foreach (System.Reflection.MethodInfo mInfo in methodInfo)

      Console.WriteLine(mInfo.ToString());

      Console.WriteLine("Members:");

      System.Reflection.MemberInfo[] memberInfo = t.GetMembers();

      foreach (System.Reflection.MemberInfo mInfo in memberInfo)

      Console.WriteLine(mInfo.ToString());

      }

      }

      /*

      Output:

      Methods:

      System.Type GetType()

      System.String ToString()

      Boolean Equals(System.Object)

      Int32 GetHashCode()

      Members:

      System.Type GetType()

      System.String ToString()

      Boolean Equals(System.Object)

      Int32 GetHashCode()

      Void .ctor()

      Void .ctor(Int32, System.String)

      System.String name

      Int32 id

      */

      此示例使用 GetType 方法確定用來包含數值計算的結果的類型。這取決于結果數字的存儲要求。

      C#

      class GetTypeTest

      {

      static void Main()

      {

      int radius = 3;

      Console.WriteLine("Area = {0}", radius * radius * Math.PI);

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

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

      );

      }

      }

      /*

      Output:

      Area = 28.2743338823081

      The type is System.Double

      */

    【運算符關鍵字typeof的使用】相關文章:

    運算符關鍵字as的使用09-25

    c#運算符關鍵字is的使用10-30

    java語言運算符的使用10-02

    Java中運算符的使用10-17

    java的import關鍵字的使用08-17

    C語言關鍵字const的使用09-02

    C語言的關鍵字define的使用08-03

    C語言的關鍵字enum的使用09-24

    C語言關鍵字static的使用09-15

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人