C# program to create a user defined the namespace

Here, we are going to learn how to create a user defined the namespace in C#?
Submitted by Nidhi, on September 11, 2020

A namespace is used to logically group similar types of classes, structures, interfaces, delegates, etc. Her we will create a user define namespace using the namespace keyword.

Program:

The source code to create a user define namespace is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to create a user defined the namespace

using System;

namespace Test.Namespace
{
    class Sample
    {
        public static void Swap(ref int X, ref int Y)
        {
            int Z = 0;

            Z = X;
            X = Y;
            Y = Z;
        }
    }
}
class Program
{
    static void Main()
    {
        int X = 10;
        int Y = 20;

        Console.WriteLine("Before swapping : X " + X + ", Y " + Y);
        Test.Namespace.Sample.Swap(ref X, ref Y);

        Console.WriteLine("After swapping  : X " + X + ", Y " + Y);
        Console.WriteLine();
    }
}

Output:

Before swapping : X 10, Y 20
After swapping  : X 20, Y 10

Press any key to continue . . .

Explanation:

In the above program, we created a user-defined namespace "Test.Namespace" that contains a Sample class. The Sample class contains a Swap() method. The Swap() method will interchange the values of parameters with each other.

Here we also created a Program class that contains the Main() method. In the Main() method, we created two local variables X and Y. Then swap the values using the Swap() method and print the swapped value on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.