Nullable Types (?) (C#)

  • Nullable Value Type

The ? suffix is a shorthand for creating an instance of the generic System.Nullable<T> structure.

static void NullableItems()
{
    int? item1 = 0;
    // Nullable<int> item1 = 0;
    bool? item2 = null;
    // Nullable<bool> item2 = null;
    char?[] arrChar=new char?[5];
    // Nullable<int>[] arrChar = new Nullable<char> [5]
}

nullableVariable.HasValue

NullableItems();

static void NullableItems()
{
    int? item1 = 0;
    // Nullable<int> item1 = 0;
    bool? item2 = null;
    // Nullable<bool> item2 = null;
    char?[] arrChar=new char?[5];
    // Nullable<int>[] arrChar = new Nullable<char> [5]

    string result1 = (item1 != null) ? ($"The value of item1 is {item1}") :
        ("The value of item1 has not been defined.");
    Console.WriteLine(result1);

    string result2 = (item2.HasValue) ? ($"The value of item2 is {item2}") :
    ("The value of item2 has not been defined.");
    Console.WriteLine(result2);

    Console.ReadLine();
}
  • Nullable Reference Type (C# 8.0)

 To enable nullable reference type, modify the value in project file as follows:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>
ValueRemarks
enableNullable annotations enabled, nullable warnings enabled.
warningsNullable annotations disabled, nullable warnings enabled.
annotationsNullable annotations enabled, nullable warnings disabled.
disableNullable annotations disabled, nullable warnings disabled.

Use compiler directives to enable or disable nullable contexts

using ConsoleApp11;

Person? p = null;
string? nullString = null;

#nullable disable
// Warning: The annotation for nullable reference types
// should only be used in code within a '#nullable' annotations
Person? person1 = null;
#nullable restore
Value of nullable compiler directiveRemarks
enableAnnotations enabled, warnings enabled
disableAnnotations disabled, warnings enabled
restoreRestore all settings to project settings
disable warningsonly disable warnings
enable warningsonly enable warnings
restore warningsonly restore warnings
disable annotationsonly disable annotations
enable annotationsonly enable annotations
restroe annotationsonly restore annotations

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
'Shining a bright light into many of the dark corners of C# 3.0, this book not only covers the 'how,' but also the 'why,' arming the reader with many field-tested methods for wringing the most from the new language features, such as LINQ, generics, and multithreading. If you are serious about developing with the C# language, you need this book.' -Bill Craun, Principal Consultant, Ambassador Solutions, Inc. 'More Effective C# is an opportunity to work beside Bill Wagner. Bill leverages his knowledge of C# and distills his expertise down to some very real advice about programming and designing applications that every serious Visual C# user should know. More Effective C# is one of those rare books that doesn't just regurgitate syntax, but teaches you how to use the C# language.' -Peter Ritchie, Microsoft MVP: Visual C# 'More Effective C# is a great follow-up to Bill Wagner's previous book. The extensive C# 3.0 and LINQ coverage is extremely timely!' -Tomas Restrepo, Microsoft MVP: Visual C++, .NET, and Biztalk Server 'As one of the current designers of C#, it is rare that I learn something new about the language by reading a book. More Effective C# is a notable exception. Gently blending concrete code and deep insights, Bill Wagner frequently makes me look at C# in a fresh light-one that really makes it shine. More Effective C# is at the surface a collection of very useful guidelines. Look again. As you read through it, you'll find that you acquire more than just the individual pieces of advice; gradually you'll pick up on an approach to programming in C# that is thoughtful, beautiful, and deeply pleasant. While you can make your way willy-nilly through the individual guidelines, I do recommend reading the whole book-or at least not skipping over the chapter introductions before you dive into specific nuggets of advice. There's perspective and insight to be found there that in itself can be an important guide and inspiration for your future adventures in C#.' -Mads Torgersen, Program Manager, Visual C#, Microsoft 'Bill Wagner has written an excellent book outlining the best practices for developers who work with the C# language. By authoring More Effective C#, he has again established himself as one of the most important voices in the C# community. Many of us already know how to use C#. What we need is advice on how to hone our skills so that we can become wiser programmers. There is no more sophisticated source of information on how to become a first-class C# developer than Bill Wagner's book. Bill is intelligent, thoughtful, experienced, and skillful. By applying the lessons from this book to your own code, you will find many ways to polish and improve the work that you produce.' -Charlie Calvert, Community Program Manager, Visual C#, Microsoft In More Effective C#, Microsoft C# MVP and Regional Director Bill Wagner introduces fifty brand-new ways to write more efficient and more robust software. This all-new book follows the same format as Wagner's best-selling Effective C# (Addison-Wesley, 2005), providing clear, practical explanations, expert tips, and plenty of realistic code examples. Wagner shows how to make the most of powerful innovations built into Microsoft's new C# 3.0 and .NET Framework 3.5, as well as advanced C# language capabilities not covered in his previous book. Drawing on his unsurpassed C# experience, the author reveals new best practices for working with LINQ, generics, metaprogramming, and many other features. He also uncovers practices that compromise performance or reliability and shows exactly how to avoid them. More Effective C# shows how to * Use generics to express your design intent more effectively* Master advanced generics techniques, such as constraints, method constraints, and generic specialization* Use the multithreaded techniques you'll need to work with the .NET framework every day* Express modern design idioms using the rich palette of C# language features* Successfully mix object oriented and functional programming constructs* Create composable interfaces and avoid confusion in public interfaces* Use extension methods to separate contracts from implementation* Program successfully with C# closures and anonymous types* Write more effective LINQ queries* Make the most of LINQ Lazy Evaluation Queries and Lambda Expressions* Distinguish and convert between delegates and expression trees* Efficiently utilize nullable types and partial classes* Use implicit properties for mutable, nonserializable data You're already a successful C# programmer-this book can help you become an outstanding one.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值