Name spaces in .netframework - DotNet and DataBase

Monday, 2 July 2018

Name spaces in .netframework





The namespace is a logical container of classes, delegates, and interfaces.

Instead of maintaining all classes at one place we can separate them into different parts logically.
Otherwise, the code becomes clumsy sometimes
So, to make a developer life easy, we separate code under different namespaces, that makes easy utilization of code.


Let us suppose I'm dealing with two subjects mathematics and physics.
I have some methods in same class and namespace related to the above subjects as below



namespace Allsubjects
{
    class subjects
    {
      
        public void add()
        {
            Console.WriteLine("Iam add");
            Console.Read();
        }
        public void substraction()
        {
            Console.WriteLine("Iam substraction");
            Console.Read();
        }
        public void multiplication()
        {
            Console.WriteLine("Iam multiplication");
            Console.Read();
        }

        public void newton1stlaw()
        {
            Console.WriteLine("Iam Newtons first law");
            Console.Read();
        }
        public void newton2ndlaw()
        {
            Console.WriteLine("Iam Newtons second law");
            Console.Read();
        }
        public void newton3rdlaw()
        {
            Console.WriteLine("Iam Newtons third law");
            Console.Read();
        }

    }
}


and I'm a mathematics subjects developer and I want to use my respective subject related methods only.
I need to create an object to the above class as below

Allsubjects.subjects s1 = new Allsubjects.subjects();
if I try to access the object s1 to invoke any the methods, .net intellisence gives us the list of available methods under that particular class as below



As a mathematics-related class developer, newton1stlaw,newton2ndlaw, and newton3rdlaw are not required to me.
So, if I'm able to separate the code  I can get the methods which are I required only.
To achieve that, I separate the code below.




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



namespace physics
{
    class allsubjects
    {
        public void newton1stlaw()
        {
            Console.WriteLine("Iam Newtons first law");
            Console.Read();
        }
        public void newton2ndlaw()
        {
            Console.WriteLine("Iam Newtons second law");
            Console.Read();
        }
        public void newton3rdlaw()
        {
            Console.WriteLine("Iam Newtons third law");
            Console.Read();
        }    

    }
}

namespace mathematics
{
    class allsubjects
    {      
        public void add()
        {
            Console.WriteLine("Iam add");
            Console.Read();
        }
        public void substraction()
        {
            Console.WriteLine("Iam substraction");
            Console.Read();
        }
        public void multiplication()
        {
            Console.WriteLine("Iam multiplication");
            Console.Read();
        }

    }
}

namespace FirstApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            physics.allsubjects s1 = new physics.allsubjects();
            mathematics.allsubjects s2 = new mathematics.allsubjects();
            Program p = new Program();
            s1.newton1stlaw();
            s2.substraction();
        }
    }

}


so, if I create an object for physics intellisence gives only my subject related methods list itself.


Practice

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.