1. Refer to namespaces at the top of the file, such as: using System;
2. Defining aliases for namespaces or types;
If the namespace is too long, it can be cumbersome to type, and if the namespace is called more than once in code, it is wise to define the alias for the namespace, and to avoid class name collisions! is not very witty ah?!
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;//define the alias "Elsename" for the namespaceusingElsename =This.Is.Very.Very.Long.NamespaceName;//defining aliases for class definitionsusingElsecname =This.Is.Very.Very.Long.NamespaceName.ThisIsVeryVeryLongClassName; Public Partial class_default:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { //instantiating an object by alias:: Is the modifier of a namespace aliasElsename::namespaceexample Nsex =Newelsename::namespaceexample (); //instantiating an object with an aliasElsecname CN =NewElsecname (); Response.Write ("namespaces:"+ nsex.getnamespace () +"; class Name:"+CN. GetClassName ()); }}namespacethis.is.very.very.long.namespacename{classNamespaceexample { Public stringGetNamespace () {return This. GetType (). Namespace; } } classThisisveryverylongclassname { Public stringGetClassName () {returnSystem.Reflection.MethodBase.GetCurrentMethod (). Declaringtype.fullname; } }}
3. Use using, define scope, and recycle resources at the end of the range.
Note The premise: the object must inherit the IDisposable interface, which is functionally equivalent to try{}catch{}finally{}.
string " Littlebai " ; // creating a Write String byte[] Bytestowrite = Encoding.Default.GetBytes (str);; // Create a file using New FileStream ("test.txt", FileMode.Create)) { // Writes a string to file 0, bytestowrite.length);}
After the execution, the program will automatically recycle the resources!
AC Group: 225443677
Use of C # using