C # namespace topic (ii)

Source: Internet
Author: User

First, namespace nesting

The previous section describes namespaces that prevent class conflicts during object instantiation of a class, if two class names are contained in two different namespaces, Then the system prompts you to choose which class to use when the instance object is created. A namespace nesting is defined as a namespace nesting that can also contain another namespace nesting. Here's a simple example to illustrate:

Using System;

Using System.Collections.Generic;

Using System.Text;

Using System.IO;

Using System.Collections.ObjectModel;

Using System.Collections;

Using System.Reflection;

Namespace csharp//the outermost namespace

{

Classprogram

{

Staticvoidmain (string[] args) {

Csharp.man Mike = new Csharp.man ()//Create man in namespace Csharp

Counter.monkey Wukong = Newglobal::csharp.counter.monkey ();

Create the. Monkey class object in the namespace counter

Counter.Nestedspace.Man Jakson = new Counter.Nestedspace.Man ();

To create the man class object in the namespace counter, you must distinguish two mans from the above method

Console.Write ("Csharp.man ' name Is{0}", Mike.name);

Console.WriteLine ("Counter.monkey ' s name is {0}", wukong.name);

Console.WriteLine ("Counter.Nestedspace.Man ' name Is{0}", Jakson.name);

Output the names of three objects

Console.readkey ();

}

}

Namespace Csharp

{

Classman

{

Publicstring name;

Public Mans ()

{

THIS.name = "name in Mans";

}

}

}

Namespace Counter

{

Classmonkey

{

Publicstring name;

Public Monkey ()

{

THIS.name = "name in monkey";

}

}//namespace Nestedspace nested inside namespace counter

Namespace Nestedspace

{

Classman

{

Publicstring name;

Public Mans ()

{

THIS.name = "nested name";

}}}}}

}

Results of output:

Two, same namespace two class method of the same name

Now add the following method to the two man classes, and look at the result of their object calling this method

public void ChangeName (string newName)

{this.name = NewName;}

Mike.changename ("Andy Lau"); Jakson.changename ("Mike Jachson")

Three Alias namespaces

At the beginning of the program reference:

Using NewName = Csharp.csharp;

Alias, original name.

Let's look at a simple example of adding code to the above program:

Using NewName = csharp.csharp;//Reference

Console.WriteLine ("Using NewName = Csharp.csharp;");

Newname.man NewMan = new Newname.man ()//also create Mana object

Console.WriteLine ("NewMan ' name Is__{0}", Newman.name);

Newman.changename ("Guo Jing");

Console.WriteLine ("NewMan ' name Is__{0}", Newman.name);

Results of program operation:

Related Article

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.