Operator : sizeof、typeof、nameof

2019-02-13  本文已影响0人  津涵

sizeof操作符

(1)值类型的byte sizes
You can determine the size (in bytes) required on the stack by a value type using the sizeof operator.
coding:
Console.WriteLine(sizeof(int));
//This displays the number 4 because an int is 4 bytes long.
(2)只有值变量的struct
You can also use the sizeof operator with structs if the struct contains only value types.
coding:
public struct Point
{
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; }
public int Y { get; }
}
(3)不建议用在类class中,会导致不安全代码
By default, unsafe code is not allowed. You need to specify the AllowUnsafeBlocks in the csproj project file.

typeof操作符

(1)typeof(string) returns a Type object representing the System.String type

nameof操作符

(1)This operator accepts a symbol, property, or method and returns its name.

上一篇下一篇

猜你喜欢

热点阅读