C # namespace Topic (i)

Source: Internet
Author: User

A namespace-namespaces

Namespaces play a big role in C #, which indicates which namespaces you use in the class, such as the Mail class in the namespace using System.Net.Mail and using System.Web.Mail exist, if you reference these two spaces in the program, but when you create the object of the mail class without indicating the class in which space is used, an error occurs:

Using System.Net.Mail;

Using System.Web.Mail;

public partial class Dmanage:System.Web.UI.Page

{

protected void Page_Load (object sender, EventArgs e)

{

MailMessage mm = new MailMessage ();

Mm. from = "liufch@163.com";

}

}

The following error occurred while compiling the current code: "MailMessage" is an ambiguous reference between "System.Net.Mail.MailMessage" and "System.Web.Mail.MailMessage"

Of course, in the development of large-scale projects need to maintain the developer-written class library, referencing the namespace can facilitate the management of the name of the same class can be specified when the specific class. Let's take a look at the next simple namespace

namespace demostrator//name of the namespace

{

Class for public class Demo//namespaces

{

private string name;

Public Demo ()//constructor

{

Name = "Luxiaofeng";

}

Public Demo (string name)//constructor

{

THIS.name = name;

}

public string GetName ()

{

return this.name;

}

}

Class for public class MyName//namespaces

{

private string name;

Public myname ()//constructor

{

Name = "Bianceng";

}

Public myname (string name)//constructor

{

THIS.name = name;

}

public string GetName ()

{

return this.name;

}

}

}

Since name is private, initializing this class in other classes will have to use this function public string getname () If you want to get name.

The following initializes an object:

Using demostrator;//references a custom space

{

protected void Page_Load (object sender, EventArgs e)

{

Demostrator. Demo Mydemo = new demo ();//Create Object

Demostrator. MyName name = new MyName ();//Create Object

Response.Write ("in Demo default name is" + mydemo.getname ());

Response.Write ("___________________");

Response.Write ("in myname default name is" + name.getname ());

}

}

The results of the page output are:

The above results are displayed in the Web page.

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.