C # Entry-level usage for namespaces

Source: Internet
Author: User

In a namespace, you can declare one or more of the following types:

Another namespace

Class

Interface

Structure

Enumeration

Commissioned

The compiler adds a default namespace whether or not you explicitly declare a namespace in the C # source file. The unnamed namespace, sometimes called the global namespace, exists in each file. Any identifier in the global namespace can be used in a named namespace.

Namespaces implicitly have public access, and this is not modifiable. Discussion on the access modifiers that can be assigned to elements in a namespace


Grammar

Namespace Samplenamespace
{
Class sampleclass{}
Interface sampleinterface{}
struct samplestruct{}
Enum Sampleenum{a,b}
delegate void sampledelegate (int i);
Namespace samplenamespace.nested
{
Class sampleclass2{}
}
}


Declaring a Namespace

Namespace Counter {
class MyClass {
}
}

Look at a namespace instance

using System;

Namespace Counter {
class MyClass {
}
}

Class MainClass {
public static void Main () {
Counter.myclass my = new Counter.myclass ();
}
}

Demo Two

Class Example {

public static void Main () {

Console.WriteLine is fully qualified.
System.Console.WriteLine ("A simple C # program.");
}
}

Re-renaming

Using Thatconsoleclass = System.Console;

Class MainClass
{
public static void Main ()
{
Thatconsoleclass.writeline ("Hello");
}
}

Multiple class calls on the same page differ in namespaces

Using system;    

Namespace Foo {

  public class Money {
    Public $ () {
   }
    public void print () {
   ;    Console.WriteLine ("Foo.money.print");
   }
 }
}

Namespace bar {

  public class $ {
    public money () {
 &N bsp; }
    public void print () {
      Console.WriteLine (" Bar.money.print ");
   }
 }
}


public class MainClass {
  public static void main () {
    Foo.money FM = new Foo.money ();
    Bar.money BM = new Bar.money ();
    fm.print ();
    bm.print ();
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.