Why is typeof needed?

后端 未结 6 1387
独厮守ぢ
独厮守ぢ 2021-01-12 14:57

Something I\'ve been thinking about from time to time: Why is the typeof operator needed in C#? Doesn\'t the compiler know that public class Animal is a type ju

6条回答
  •  甜味超标
    2021-01-12 15:13

    typeof(Class) is the only way to express Type as a literal. When you write Class.SomeField you mean static field. When you write typeof(Class).SomeField you reference field of object of class Type that represents your class.

提交回复
热议问题