Daily Academy C # - Using

xiaoxiao2021-03-06  63

Dear C #, the using instruction can be used to reference the namespace, such as: use system; you can also specify an alias named space or class, such as: // Extract MSDN // cs_using_directive.csusing myalias = mycompany.proj.nested ; // define an alias to represent a namespace

Namespace mycompany.proj {public class myclass {public static void donothing () {}}

Namespace Nested // a Nested Namespace {Public Class ClassinnestedNamespace {PUBLIC Static Void Sayhello () {System.Console.writeline ("Hello");}}}

}

public class UnNestedClass {public static void Main () {MyAlias.ClassInNestedNameSpace.SayHello (); // using alias}} may be used to specify the alias class: // cs_using_directive2.csusing System; // using directiveusing AliasToMyClass = NameSpace1.MyClass ; // USING Alias ​​for a class

Namespace namespace1 {public class myclass {public override string toString () {return "you are in namespace1.myclass";}}}

Namespace namespace2 {class myclass {}}

Namespace namespace3 {using namespace1; // using directive using namespace2; // using Directive

Class test {public static void main () {aliastomyClass Somevar = new aliastomyclass (); console.writeline (Somevar);}}} It is a using statement today. If you have heard it, you have never seen it. You can see the basic usage of the USING statement Using (some instances) {// using these instances} // Dispose these instances by Compiler Braces contains instances referenced in {}, after exiting the USING statement, these objects are reclaimed (Dispose ) Example: Using system.drawing; class a {public static void main () {use (font myfont = new font ("arial", 10.0f), myfont2 = new font ("arial", 10.0f)) {//// Use myfont and myfont2} // compiler will call dispose on myfont and myfont2font myfont3 = new font ("arial", 10.0f); using (myfont3) {// use myfont3} // Compiler Will Call Dispose On MyFont3

}

转载请注明原文地址:https://www.9cbs.com/read-83497.html

New Post(0)