c# – Using the “Null” object

Sometimes a method will ask for an input parameter, but that input parameter is optional. In these situation you can still trigger the method, without passing an input parameter, and instead pass in a “null” object in it’s place:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExceptionExample
{
    class Program
    {
        static void Main(string[] args)
        {

            FirstMethod("hello world");
            // FirstMethod();           // this will cause an error because the "firstmethod" method must require
										// an input parameter in order to operate
            FirstMethod(null);

        }

        public static void FirstMethod(string message)
        {
            if(String.IsNullOrEmpty(message))
            {
                Console.WriteLine("There is no message that has been passed in");
            }
            else
            {
                Console.WriteLine("The message is: " + message);
            }
            
        }

    }
}