Using keyword PDF Print E-mail
User Rating: / 0
PoorBest 
Monday, 09 March 2009
HTML clipboard

using

The using directive can be used in 2 different ways:

  • as an alias for a namespace
  • allows you to use types of a particular namespace without the need for qualifying the entire namespace
using [alias = ] class_or_namespace;

where:

alias(optional) - a user defined symbal that is representing a namespace. class_or_namespace - is the namespace name that you wish to either use or alias, or the class name that you wish to alias. Key Points:

Common use of using is to make life easier so you don't have to fully quality ever statement.

Create a using directive to use the types in a namespace withough having to specify that actual namespace.

A using directive does not give you access to any of the various namespaces that might be nested in the namespace that you specified.

A user defined namespace is referring to any namespace you have created in yuor code.

Example:
// using keyword
using System;
using CSF = CSharpFriends.BestResource.ForCSharp;  // alias

namespace CSharpFriends.BestResource
{
    public class MyClass
    {
        public static void DoNothing()
        {
            //empty
        }
    }

    namespace ForCSharp
    {
        public class MyNestedClass
        {
            public static void MyMessage()
            {
                System.Console.WriteLine("CSharpFriends!");
            }
       
        }       
   
    }

}
public class TestUsing
{
    public static void Main()
    {
        CSF.MyNestedClass.MyMessage();
       
        // pauses output window
        Console.ReadLine();
    }
}

 
< Prev   Next >
School Joomla Templates and Joomla Tutorials