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;
}
}
}
C# Nullable 使用一例
最新推荐文章于 2024-07-10 10:47:44 发布