創作內容

1 GP

Unity C# is、as、強轉換、typeof運算子功能說明

作者:蝦米coco│2020-05-24 16:03:32│巴幣:2│人氣:1072
is運算子
功能:檢查運算式的執行階段型別是否與給定型別相容

E is T

限制
1.E 是會傳回值的運算式,而 T 是型別名稱或型別參數。
2.E 不能是匿名方法或 Lambda 運算式。
3.如果E 的結果是非 Null 且可轉換成型別 T (藉由參考轉換、Boxing 轉換或 Unboxing 轉換),則 E is T 運算式傳回true;否則傳回 false
4.is 運算子不會考慮使用者定義轉換。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Exercisc : MonoBehaviour
{
public class A
    {

    }
    public class B : A
    {

    }

    private void Start()
    {
        A a = new A();
        B b = new B();
        A ab = new B();
        Debug.Log("a is A ="+ (a is A));
        Debug.Log("b is A =" + (b is A));
        Debug.Log("ab is A =" + (ab is A));
        Debug.Log("a is B =" + (a is B));
        Debug.Log("b is B =" + (b is B));
        Debug.Log("ab is B =" + (ab is B));
//
a is A =True
b is A =True
ab is A =True
a is B =False
b is B =True
ab is B =True
    }
}

E is T v
E is T v 運算式會傳回 true,E 的結果會指派至變數v(可以使用在if子句)

as運算子
功用:as運算子將運算式的結果明確地轉換成給定參考或可為 Null 的實值型別。
如果無法轉換,則 as 運算子會傳回 null
強制轉換表示式不同as, 運算子從不引發異常。

限制
1.as 運算子只考慮參考、可為 Null、Boxing 和 Unboxing 轉換
2.您無法使用as 運算子來執行使用者定義轉換。 不過可以使用強制轉換表示式解決。

E as T//和這個程式碼是一樣的E is T ? (T)(E) : (T)null
其中 E 是會傳回值的運算式,而 T 是型別名稱或型別參數

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Exercisc : MonoBehaviour
{

    private void Start()
    {
        int[] num = new[] { 15, 25, 35 };
        IList<int> list_num = num as IList<int>;
        if (list_num != null)
        {
            Debug.Log(list_num[0]+"和"+list_num[1]+"和"+list_num[2]);
        }
//15和25和35
    }
}

轉換運算式

功用:格式為 (T)E 的轉換運算式會執行明確轉換,將運算式 E 的結果轉換成型別 T
如果沒有從型別E 轉換成型別 T 的明確轉換存在,就會發生編譯時期錯誤。
在執行階段,明確轉換可能不會成功,且轉換運算式可能會擲回例外狀況。
double a = 8787.7777;int a = (int)b;
Debug.Log(a);//8787

typeof運算子
功用:取得型別的 System.Type 執行個體

1.typeof 運算子的引數必須是型別名稱或型別參數如int,string 等類型及自定義類型,不能是實例。
2.您還可以使用運算子typeof與未綁定的泛型類型
3.運算式不能作為 typeof 運算子的引數
4.Gettype返回的是實例的type(類型),而typeof返回的是Class(類)的類型。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Exercisc : MonoBehaviour
{
    public delegate Func<int,string> Del_A();
    public class A
    {

    }
    public class B : A
    {

    }

    private void Start()
    {
        A a = new A();
        B b = new B();
        A ab = new B();
        Debug.Log("a is A =" + (a is A));
        Debug.Log("b is A =" + (b is A));
        Debug.Log("ab is A =" + (ab is A));
        Debug.Log("a is B =" + (a is B));
        Debug.Log("b is B =" + (b is B));
        Debug.Log("ab is B =" + (ab is B));
        Debug.Log("~~~~~~~~~~~~~~~~~~~~~~~~");
        Debug.Log("typeof(A) =" + typeof(A));
        Debug.Log("typeof(B) =" + typeof(B));
        Debug.Log("typeof(string) =" + typeof(string));
        Debug.Log("typeof(string) =" + typeof(Del_A));
        Debug.Log("~~~~~~~~~~~~~~~~~~~~~~~~");
        Debug.Log(a.GetType() ==typeof(A));
        Debug.Log(b.GetType() == typeof(A));
        Debug.Log(ab.GetType() == typeof(A));
        Debug.Log(a.GetType() == typeof(B));
        Debug.Log(b.GetType() == typeof(B));
        Debug.Log(ab.GetType() == typeof(B));
//
a is A =True
b is A =True
ab is A =True
a is B =False
b is B =True
ab is B =True
~~~~~~~~~~~~~~~~~~~~~~~~
typeof(A) =Exercisc+A
typeof(B) =Exercisc+B
typeof(string) =System.String
typeof(string) =Exercisc+Del_A
~~~~~~~~~~~~~~~~~~~~~~~~
True(不能和string一起使用Debug.Log("No"ab.GetType() ==typeof(B));不行)
False
False
False
True
True

    }
}
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4793324
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

1喜歡★pk123634 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:Unity C# 事件 ... 後一篇:...

追蹤私訊切換新版閱覽

作品資料夾

pauk0308台股
不要再跌了拜託看更多我要大聲說昨天22:05


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】