How and Why .NET Namespace used?

Today in Engineer's World, we are discussing a very important topic of C# - Namespaces in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS

Click imagination hunt to see latest Blogs


What is Namespace in C#?

A Namespace is collection of classes with related type contained in an assembly file. NET Framework provides namespaces with classes that can be used to code in various tasks. The .NET Framework class library can help you implement many tasks, including accessing databases, creating graphics, XML processing, networking, messaging, security, and directory services. Some commonly used namespaces during our initial start-up are listed below:
 
Namespaces in Asp.net
 
When referencing a class, one should specify either its fully qualified name, which means namespace followed by the class name, or add a using statement. This, eliminates the need to mention the complete name of all classes in that namespace.

/*You can write like this.*/
System.Console.WriteLine("Hello Engineer's!");
 /*Or, you can write like this.*/
using System; 
Console.WriteLine("Hello Engineer's!"); 

Before Proceeding, to learn various namespaces.
I encourage beginners to watch Basics of programming.

.NET Namespaces:

1. System: Contain the core classes. This namespace is used for following attribute like mathematical computations, Garbage collection, Intrinsic data, Exceptions and attributes.

using System;

namespace Engineer's_World
{
    class Namespaces
    {
        public static void Main()
        {
           
Console.WriteLine("Welcome to the Engineer's World!");
        }
    }
}

Output:
Welcome to the Engineer's World!

2. System.Collection: Contain collection classes and interfaces. This namespace is used when we are using collections (Generic and Non Generic), i.e., List, Stack, Queue, Hasttable, INumerable, ICollection, IDictionary.

3. System.Data: Classes for accessing Database. This Namespace is used when we are using Database connectivity in Ado.net Application. If any Namespace use "Data" attribute, then it will use for Database connectivity in .NET and may come under System.Data.

using System;
using System.Data;

namespace Engineer's_World
{
    class Namespaces
    {
        public static void Main()
        {
           
DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Type", typeof(string));

            // Here we add two DataRows.
            table.Rows.Add("Engineer's", "Profession");
            table.Rows.Add("World", "Education");
            Console.WriteLine(table.Rows[0].Field<string>(1));
        }
    }
}

Output:
Engineer's


4. System.Data.SqlClient: Classes for accessing Database. This also used for database connectivity in Ado.net. When we use SQL query for data connection.

using System;
using System.Data.SqlClient;

namespace Engineer's_World
{
    class Namespaces
    {
        public static void Main()
        {
             SqlConnection con = new SqlConnection(System.ConfigurationManager.ConnectionStrings["connstring"].ConnectionString);
        con.Open();
       
SqlCommand cmd = new SqlCommand("select Username from UserLogin where Username='" + TextBoxU.Text + "' and Password='" + TextBoxP.Text + "'", con);

        }
    }

5. System.Configuration: When we configure some .net controls and use it in our .Net application. then we use this Namespace. Ex. Configuration of SQL Data Source or SQL Data Adapter and use it for connection.

using System;
using system.Data.SqlClient;
using System.Configuration;

namespace Engineer's_World
{
    class Namespaces
    {
        public static void Main()
        {
             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString);
        //con.Open();
        //SqlCommand cmd = new SqlCommand("select Username from UserLogin where Username='" + TextBoxU.Text + "' and Password='" + TextBoxP.Text + "'", con);
        }
    }
}
 


6. System.Windows.Forms: Classes for graphics and design of Windows Forms. When we are making any GUI based application in .NET then we use this namespace.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Engineer's_World
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void label1_Click(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.label1.Font = new Font("Arial", 12.0f);
            this.label1.Text = "Engineer's World";
        }
    }
}

7. System.Drawing: Classes for graphics and design of Windows Forms. This namespace is used for Graphical Primitive data type such as size, fonts and printing services etc.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Engineer's_World
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void label1_Click(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.label1.Font = new Font("Arial", 12.0f);
            this.label1.Text = "Engineer's World";
        }
    }
}


8. System.IO: Classes for disk Input/Output.

using System;
using System.IO;


namespace Engineer's_World
 {
    class Class1
    {
        public static void Main(String[] args)
        {
            using (
StreamWriter writer = new StreamWriter("D:\\out.txt"))
            {
                Console.SetOut(writer);
                Act();
            }
        }

        static void Act()
        {
            Console.WriteLine("This is Console.WriteLine");
            Console.WriteLine("Thanks for playing!");
        }
    }
}

9. System.Text: To process string in classes.

using System;
using System.Text;

namespace Engineer's_World
{
    class String_Builder
    {
        public static void Main()
        {
            /*Task- Replace and Remove method in StringBuilder*/
           
StringBuilder MyStr = new StringBuilder("Enginner's World!");
            Console.WriteLine("Initial Call:" + MyStr);
            MyStr.Replace("ne", "ee");
            Console.WriteLine("Call 2: " + MyStr);
            MyStr.Remove(5, 2);
            Console.WriteLine("Call 1: " + MyStr);
        }
    }
}


10. System.Threading: Thread support in .NET. This namespace is used when we are making a multi-threading application in .Net.

11. System.Xml: When we are using XML Language in our programs then we will use this Namespace in our programs.

using System;
using System.Text;
using System.Xml;


namespace Engineer's_World
{
    class String_Builder
    {
        public static void Main()
        {
            StringBuilder str = new StringBuilder();
            str.Append("<head>");
            str.Append("<name>Harry</name>");
            str.Append("<id>1</id>");
            str.Append("</head>");
            string Mystr = str.ToString();

            StringBuilder str1 = new StringBuilder();
            str1.Append("<head>");
            str1.Append("<name>Potter</name>");
            str1.Append("<id>2</id>");
            str1.Append("</head>");
            string Mystr1 = str1.ToString();

           
XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(Mystr);

           
XmlDocument xdoc1 = new XmlDocument();
            xdoc1.LoadXml(Mystr1);

            string name = xdoc.SelectSingleNode("head/name").InnerText;
            xdoc1.SelectSingleNode("head/name").InnerText = name;
            Mystr1 = xdoc1.OuterXml;
            Console.WriteLine(Mystr1);
        }
    }
}



[Note]: Most Important thing to keep in mind while using Namespaces. Usage will be done according to their requirement. Avoid unnecessary implementation of namespaces as it increases system overload.


For any query, comment us below.


Related Questions:


Q-1 Which of the following is NOT a namespace in the .NET Framework Class Library?
A) System.Process.
B) System.Security
C) System.Threading
D) System.Drawing
E) System.Xml
Ans. Option(A).

Q-2 Which of the following statments are the correct way to call the method Issue() defined in the code snippet given below? 

using System;
using System.Text.RegularExpressions;

namespace AspnetSolutions
{
  namespace College
   {
      namespace Lib
      {
         class Book
          {
              public void Issue()
              {
                  // Implementation code
              }
          }
          class Journal
          {
              public void Issue()
              {
                  // Implementation code
              }
          }
      }
   }
}

1. College.Lib.Book b= new College.Lib.Book();
    b.Issue();
2. Book b=new Book();
    B.Issue();
3. using College.Lib;
    Book b =new Book();
    b.Issue();
4. using College;
    Lib.Book b=new Lib.Book();
    b.Issue();
5.using Coleege.Lib.Book;
    Book b=newBook();
    b.Issue();
A) 1, 3
B) 2, 4
C) 3    
D) 4, 5
Ans. Option (A).

Q-3 Which of the followings are NOT a .NET namespace? 
1. System.Web;
2. System.Process;
3. System.Data;
4. System.Drawing2D;
5. System.Drawing3D;
A) 1, 3
B) 2, 4, 5
C) 3, 5
D) 1, 2, 3
Ans. Option (B).

Click imagination hunt to see latest Blogs

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions