语法依赖于哪些 C# 类型?
可能的重复:
C# .NET 的哪些部分框架实际上是语言的一部分?
我知道它使用
System.Type
就像typeof(foo)
-
System.Attribute
因为你可以使用[Foo]
而不是[FooAttribute]
-
System.String
因为您可以编写"bla".ToLower()
。 -
System.Exception
因为throw obj
仅适用于此类型或继承类型。 -
System.Array
显然 - 还有所有内置值类型
int
、decimal
、bool
等。
还有其他的吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个很难的问题。好吧,我将从您可以在源代码中编写的、不需要在运行时计算的文字值开始:
"String"
'c'
十进制
、浮点
、双精度
int
、uint
、长
,ulong
(short
、ushort
、byte
和sbyte
也是受支持的关键字,但与这四个不同,它们需要强制转换)true
|false
) (也隐式用于评估,例如a == b
或c)
null
文字(实际上不是类型,但为了完整性起见,我将其放在这里,因为它可以表示任何和所有引用类型。)现在,语言构造:
bool[]
char[]
float[]
etc (支持上面显示的所有类型,除了 null 以及所有用户定义的类型)类型bool?
char?
float?
etc (框定值类型以允许额外值 null)typeof(T)
- 正如你所说,返回Type,所以c#语言支持Type类型。此外,对于每个对象引用或值类型,您都可以对其调用 GetType() 来检索相同的内容 - 这是从 object 继承的。IEnumerable
、IEnumerator
、IEnumerable
和IEnumerator
Func
,Func
、Action
、Action
、Action
用于 lambda 表达式。当我想到它们时,我会更新更多内容。
This is a hard question. Well, I'll start with the literal values that you can write in the source code that doesn't require evaluation at runtime:
"String"
'c'
decimal
,float
,double
int
,uint
,long
,ulong
(short
,ushort
,byte
, andsbyte
are all supported keywords also, but they require a cast, unlike these four)true
|false
) (also implicitly used in evaluations such asa == b
orc < d
)null
literal (not actually a type, but I put it here in the interest of completeness because it can represent any and all reference types.)Now, language constructs:
bool[]
char[]
float[]
etc (support all types shown above except null plus all user-defined typesbool?
char?
float?
etc (boxes valuetypes to allow the extra value null)typeof(T)
- as you said, returns Type, so c# language supports the Type type. Also, for every single object reference or value type, you can call GetType() on it to retrieve the same thing - this is inherited from object.IEnumerable
,IEnumerator
,IEnumerable<T>
andIEnumerator<T>
for foreach and query expressionsSystem.Threading.Monitor
for lock () statementsFunc<T>
,Func<T1, T2, ... TN>
,Action
,Action<T>
,Action<T1, T2, ... TN>
for lambda expressions.IDisposable
interface for using() statementsException
class for catch (ExceptionClassType) {} clauses in try statements.I'll update with more when I think of them.
根据我对您问题的理解,您想要创建 C# 编译器需要了解的预定义类列表以支持 C# 语言功能。
从我的脑海中,我会添加以下内容:
System.Object
,基类,System.ValueType
,所有struct
都来自于它> 继承System.Delegate
,委托类型的基类。From how I understand your question, you want to create a list of pre-defined classes that the C# compiler needs to be aware of to support C# language features.
From the top of my head, I'd add the following:
System.Object
, the base class,System.ValueType
, from which allstruct
s inherit,System.Delegate
, the base class for delegate types.不确定我完全理解这个问题,但是...
System.IDisposable
仅适用于using(var myDisposable = new MyDisposable()){...}
,其中 < code>MyDisposable 实现了IDisposable
。Not sure I totally understand the question, but...
System.IDisposable
only works withusing(var myDisposable = new MyDisposable()){...}
, whereMyDisposable
implementsIDisposable
.我将添加到列表中:
IEnumerable
、IEnumerable
、IEnumerator
和IEnumerator
作为带有yield break
和/或yield return
的方法或属性的返回类型必须是这些类型之一,并且编译器生成的类将实现它。我不同意以下逻辑:
因为这只是一些糖,这意味着如果属性的名称结束带有“Attribute”并且不存在与不带“Attribute”名称相同的其他属性,那么您可以省略该位。不过,
System.Attribute
仍然是一个示例,因为您不能将对象用作属性,除非该对象是该属性的后代。I'll add to the list:
IEnumerable
,IEnumerable<T>
,IEnumerator
andIEnumerator<T>
as the return type of a method or property withyield break
and/oryield return
must be one of those types, and the compiler-generated class will implement it.I'll disagree with the logic of:
As that is just some sugar that means that if an attribute's name ends with "Attribute" and there isn't another attribute whose name is the same as it without "Attribute", then you can omit that bit.
System.Attribute
is still an example though, since you cannot use an object for an attribute unless it's descended from it.