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.