`

C# Nullable 使用一例

    博客分类:
  • C#
阅读更多
using System;
using System.Collections.Generic;
using System.Text;

namespace Nullable
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Nullable Data ******");
            DatabaseReader dr = new DatabaseReader();

            int? i = dr.GetIntFromDatabase();
            if (i.HasValue)
            {
                Console.WriteLine("Value of i is: {0}", i);
            }
            else
            {
                Console.WriteLine("Value of i is undefined.");
            }

            bool? b = dr.GetBoolFromDatabase();
            if (b != null)
            {
                Console.WriteLine("Value of b is: {0}", b);
            }
            else
            {
                Console.WriteLine("Value of b is undefined");
            }

            int? myData = dr.GetIntFromDatabase() ?? 100;
            Console.WriteLine("Value of myData: {0}", myData);
            Console.ReadLine();
        }
    }
    class DatabaseReader {
        public int? numbericValue;
        public bool? boolValue = true;

        public int? GetIntFromDatabase()
        {
            return numbericValue;
        }

        public bool? GetBoolFromDatabase()
        {
            return boolValue;
        }
    }
}

分享到:
评论

相关推荐

    javax Nullable 标签的jar

    使用javax Nullable注解必须使用的jar包

    Nullable转换问题

    用例子的形式详解了Nullable的用法

    详解c# 可空类型(Nullable)

    C# 单问号 ? 与 双问号 ?? ? : 单问号用于对 int,double,bool 等无法直接赋值为 null 的数据类型进行 null 的赋值,意思是这个数据...C# 提供了一个特殊的数据类型,nullable 类型(可空类型),可空类型可以表示其基

    Laravel开发-nullable

    Laravel开发-nullable 对对象进行链调用,无论对象是否为空

    C#难点逐个击破(8):可空类型System.Nullable

    null值用来表示数据类型未被赋予任何值,它是一种引用类型;void表示没有类型,或者说是没有任何值。null与void的区别可以认为void是根本没有,而null是一个空箱子,里面什么都没有。

    C# 7.0 Pocket Reference: Instant Help for C# 7.0 Programmers

    iterators, nullable types, operator lifting, lambda expressions, and closures LINQ, starting with sequences, lazy execution and standard query operators, and finishing with a complete reference to ...

    Laravel开发-laravel-nullable

    Laravel开发-laravel-nullable Laravel 5-向模型添加可空列

    Exploring Advanced Features in C#-.pdf

    • Look at features of C# 8 such as nullable reference types, recursive patterns, ranges, indices, switch expressions, and more • Create responsive web apps using ASP.NET MVC. Using SASS, jQuery, ...

    More Effective C#

    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 ...

    Programming C# 8.0_ Build Cloud, Web, and Desktop Applications

    You’ll get up to speed on .NET Core and the latest C# 8.0 additions, including asynchronous streams, nullable references, pattern matching, default interface implementation, ranges and new indexing ...

    java中使用@Nullable 注解的详细用法

    最近发现之前写的代码生成器(entity、dao、service、controller、vue) 有点bug,在Service层判断空的时候,少了一部分条件。所以补充上了,随后又同事问我在代码中发下了@Nullable注解不知道怎么用?脑子是个好...

    .NET中可空值类型【NullableT】实现原理

    下面通过自定义一个可空值类型来讲解Nullable的实现原理。 自定义可空值类型 struct XfhNullable<T> where T : struct { private T innerValue; //这个属性很重要 public bool HasValue { set; get; } public T...

    Professional C# 7 and .NET Core 2.0, 7th Edition

    C# 7 focuses on data consumption, code simplification, and performance, with new support for local functions, tuple types, record types, pattern matching, non-nullable reference types, immutable ...

    深入解析C#(第2版)

    Chapter 4 Saying nothing with nullable types Chapter 5 Fast-tracked delegates Chapter 6 Implementing iterators the easy way Chapter 7 Concluding C# 2: the final features Part 3 C# 3: revolutionizing ...

    浅谈C# 中的可空值类型 null

    但是,利用 C# 定义的一个修饰符,可将一个变量声明为一个可空(nullable)值类型。可空值类型在行为上与普通值类型相似,但可以将一个 null 值赋给它。如下所示: 代码如下:int? a = null; // 合法 当把一个变量...

    c#是什么呢?初学者看看吧

    1、 泛型:在我看来,泛型就是通过将数据类型参数化从而实现了代码的更为灵活的复用,泛型的出现使得C#能够使用同一段代码来操作多种数据类型。泛型无疑是C#2.0最重大的改进,它的出现赋予了C#代码更强的类型安全,...

    [.Net] Professional.C#.7.and.NET.Core.2.0英文原版书籍 源码11.8M,epub格式30M,pdf格式40M

    C# 7 focuses on data consumption, code simplification, and performance, with new support for local functions, tuple types, record types, pattern matching, non-nullable reference types, immutable ...

    jackson-databind-nullable-0.1.0.jar

    java运行依赖jar包

Global site tag (gtag.js) - Google Analytics