This example help you get familiar with nullable types in C#. You can pass the null values in function and it will not raise any error and you can explicitly check if it contains null values.
Source Code :
using System;
using System.IO;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
NullValueExample(200);
NullValueExample(null);
Console.ReadLine();
}
static void NullValueExample(int? Number)
{
if (Number.HasValue == true)
{
Console.WriteLine("Number has value: " + Number);
}
else
{
Console.WriteLine("Number has null value: null");
}
try
{
Console.WriteLine("Print value: = {0}", Number.Value);
}
catch (InvalidOperationException ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
}
}
No comments:
Post a Comment