Quantcast

Maximum PC

It is currently Wed Aug 27, 2014 7:06 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Got a quick question about C#
PostPosted: Sun May 31, 2009 10:43 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
What is the benefit of creating your own namespace?


Top
  Profile  
 
 Post subject: Re: Got a quick question about C#
PostPosted: Sun May 31, 2009 12:34 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
xxmainframexpxx wrote:
What is the benefit of creating your own namespace?

One major benefit is that you are preventing name clashes with system and other classes.
For example:
Microsoft's .NET classes include a class called String. It represents a string of characters, like "foo".
Now suppose that you are working on a program for a company that makes craft supplies. In a momentary lapse of reason, you create a String class to represent a ball of yarn. If you put this into your own namespace, you can still use it and the System.String class perfectly. However, if you decide to put your String class in a global namespace, it can cause some problems at compile time (And maybe runtime too, I'm not sure about that).


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jun 01, 2009 4:21 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
I use them to let me group things logically together. Most of my solutions have projects laid out like this:

ProjectName.Application
ProjectName.Common
ProjectName.DataAccess
ProjectName.Business

It works well, keeps things organized, and I only need to include the project (namespace) that I need at a given level. The application level never should need the DataAccess because the Business layer handles opening and saving the data through it's objects, etc.


Top
  Profile  
 
 Post subject: Re: Got a quick question about C#
PostPosted: Mon Jun 01, 2009 7:46 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
xxmainframexpxx wrote:
What is the benefit of creating your own namespace?


namespaces allow you to logically group related classes together. It allows you to create a hierarchy of classes in addition to relating classes.

Namespaces Tutorial


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jun 09, 2009 7:20 am 
Super Mario Banhammer
Super Mario Banhammer

Joined: Fri Aug 25, 2006 11:20 am
Posts: 595
Grouping related classes in a logical manner is always a good thing. Preventing naming clashes or ambiguity is another good reason. A third reason would be for specialized classes in the area of inheritance and class polymorphism As an example:
If you have classes in two different namespaces that one of the class relies on for input or a property of some kind then you might see something like the following:

Code:

namespace Test.CSharpExample.ImplementsA
{
     public class Hello
     {
         private string m_Message;

          public string Message
          {
              get
              {
                   return m_Message;
              }
              set
              {
                  m_Message = value;
              }
           }
     }
}



Code:
namespace Test.CSharpExample.InheritsB
{

    abstract class Hello   // Abstract class
    {
        protected string m_Message = "";
        public abstract void AbstractMethod();   // Abstract method
        public abstract string Message  {get;}{set;}

    }

}




and if you would like to derive the properties and methods of the InheritsB Hello class or instance the ImplementsA Hello class:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using Test.CSharpExample.ImplementsA;
using Test.CSharpExample.InheritsB;

namespace Test.CSharpExample.DerivedOrImplementedClasses
{

     class DerivedClass : Test.CSharpExample.InheritsB.Hello
     {
        public override void AbstractMethod()
        {
                m_Message = "Hello, World";
         }

        public override string Message   // overriding property
       {
             get
             {
                 return m_Message;
             }
             set
             {
                 m_Message = value;
             }
         }

     static void Main()
    {
        DerivedClass o = new DerivedClass();
        Test.CSharpExample.ImplementsA.Hello o2 = new   Test.CSharpExample.ImplementsA.Hello();
        o.AbstractMethod();
        o2.Message.ToString();
       
        Console.WriteLine("{0}", o.Message);
        Console.WriteLine(o2.Message.ToString());
    }
     
   }
  }




As you can see you can do both within the same operator class. With namespaces you can logically organize, provide a way to distinguish between differing subtypes of a class to avoid inconsitency or ambiguity, provide yourself with a shortcut to writing cleaner more readable code and you can provide for both inheritance and implementation within the same namespace :D


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group