51

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

Samuel Katz
  • 24,066
  • 8
  • 71
  • 57
Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57
  • 1
    In what context would you use such a thing? By that, I mean, what are you trying to accomplish? You can always check for null when using objects created using your classes in C#. and it's odd that you are using the term "type". If you added some context to your question it would be easier to figure a solution. – stephenbayer Jun 15 '11 at 23:12
  • AFAIK non nullable reference types would be possible if the assignment operator would be overloadable. This however is not the case in C#. The other catch is `MyType type;` would be null at the start. – Marino Šimić Jun 15 '11 at 23:27
  • 9
    @stephenbayer: nulls are *bad*. Run-type null checks fundamentally no different from run-type type checks. If I give you a string and you call methods on it, you won't know whether it's nullable or not until it blows up during runtime. Some languages have non-nullable reference types; if you want to represent a null, you use a different data type (equivalent to `option` found in many languages), so you communicate and enforce null-checking through the type system at compile time. See http://stackoverflow.com/questions/641328/about-the-non-nullable-types-debate/1451909#1451909. – Juliet Jun 15 '11 at 23:33
  • 1
    @Juliet agreed. I was actually just confused as to the terminology used, by calling it a Type, i didn't know if he meant class, struct, or some other user defined type, like an enum.. etc, etc. I'm used to checking everything for null before using them. but I completely see your point of the design time advantage. – stephenbayer Jun 15 '11 at 23:38
  • 2
    The *fixed* keyword creates a value type. Doesn't exactly count as a generally useful type in C# programming. – Hans Passant Jun 16 '11 at 00:24
  • 2
    possible duplicate of [How can I get close to non-nullable types in C# today?](http://stackoverflow.com/questions/2181846/how-can-i-get-close-to-non-nullable-types-in-c-sharp-today) – nawfal Jul 08 '14 at 09:54

3 Answers3

58

Yes, these are called struct.

Structs are value types, just like int, bool and others.

They have some rules/recommendations related to them: (I think these are the most important)

  • a struct is passed and assigned by value, when not using ref or out keywords... this means that everything you put inside a struct will be copied when assigning or passing it to a method. That is why you should not make large structs.

  • you cannot define a parameterless constructor for a struct in C#

  • structs are better to be immutable, and have no property setters. You can get into real trouble by making mutable structs.

Other rules can be found within Microsoft docs about structs.

As for non-nullable reference types... this is not possible. You must check for nulls inside your code, manually.

Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
26

7 years later and this is now possible

  • Install .NET Core 3.0 which includes C# 8.
  • Set the language version to 8.0: <LangVersion>8.0</LangVersion> to your .csproj file.
  • Add the property <NullableReferenceTypes>true</NullableReferenceTypes> (to your .csproj)

More details on his this affects writing code and your existing code here:

https://praeclarum.org/2018/12/17/nullable-reference-types.html

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
Brian Rosamilia
  • 1,448
  • 14
  • 24
  • 1
    Note: this is only intent, the compiler does not force the not-null things to be never-null, only warn in every place it thinks that it is possible for null to be placed into a non-nullable object. – Andrew Hill May 14 '19 at 02:33
  • 3
    In case that link goes offline, the code you need to add to your .csproj file to enable nullable reference types is: `8.0 true` – deadlydog Oct 08 '19 at 16:18
14

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.

dtb
  • 213,145
  • 36
  • 401
  • 431