Create Non-Nullable Types in C#

后端 未结 3 1033
感情败类
感情败类 2020-12-14 14:21

How to create non-nullable value types like int, bool, etc. in C#?

3条回答
  •  情书的邮戳
    2020-12-14 14:49

    You can define a struct:

    A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory. The following example shows a simple struct declaration:

    public struct Book
    {
        public decimal price;
        public string title;
        public string author;
    }
    

    However, you can't define aliases like int for System.Int32 and need to refer with the full name MyNamespace.Book (or Book with using MyNamespace;) to your struct.

提交回复
热议问题