簡單的問題。爲什麼不是表達式typeof(this)
無效的C#?或者表達式sizeof(this)
,以及?當我嘗試運行一個簡短的測試程序時,它說類型預期,然後引發一堆其他語法錯誤。我的意思是,typeof
是一個方法(或者你傳遞一個參數的關鍵字,不確定),它與sizeof
相同,那麼爲什麼不是this
是一個有效的參數呢?我的意思是,你可以用this
(例如MyMethod(this)
)調用很多方法,那麼typeof
和sizeof
是什麼特殊?爲什麼不是typeof(this)或sizeof(this)有效?
當編寫一個代碼片段如DependencyProperty
這將是非常有用的。例如,查看propa
片段。它必須在那裏使用typeof(ownerclass)
。如果它可能是typeof(this)
,那將會好很多。
測試程序:
using System;
public struct TestingClass
{
public static void Main()
{
TestingClass tc = new TestingClass();
tc.Test();
Console.ReadKey(true);
}
public void Test()
{
Console.WriteLine(typeof(this)); //error here
Console.WriteLine(sizeof(this)); //and here too
}
}
你真的是92個 – user2029077
@MinimusHeximus號 –