Saturday 18 August 2018

C#: Nested namespaces

A name space can be nested in another name space.

Program.cs

using System;

namespace Arithmetic
{
    namespace IntegerArithmetic
    {
        class Arithmetic
        {
            public static int sum(int a, int b)
            {
                return a + b;
            }
        }
    }

    namespace FloatArithmetic
    {
        class Arithmetic
        {
            public static float sum(float a, float b)
            {
                return a + b;
            }
        }
    }
   
}

class Program
{
    static void Main(string[] args)
    {
        int result1 = Arithmetic.IntegerArithmetic.Arithmetic.sum(10, 20);

        Console.WriteLine("Sum of 10 and 20 is {0}", result1);
    }

}


Output
Sum of 10 and 20 is 30


Notify above snippet, IntegerArithmetic and FloatArithmetic namespaces are defined inside Arithmetic namespace.




Previous                                                 Next                                                 Home

No comments:

Post a Comment