Using usage C #

Source: Internet
Author: User
  1. // Using is used as the namespace indicator
  2. Using system;
  3. Using system. Collections. Generic;
  4. Using system. text;
  5. Using system. IO;
  6. Namespace statement
  7. {
  8. // Specify the font class alias as F
  9. Using F = system. Drawing. Font;
  10. Class Program
  11. {
  12. Static void main (string [] ARGs)
  13. {
  14. // Usage of using statement for resource management
  15. Using (textwriter W = file. createtext ("E: // test.txt "))
  16. {
  17. W. writeline (@"Using statement usage: The using statement allowsProgramThe User specifies when the resource object should be released. Resource management statement function ");
  18. // Use an alias to instantiate an object
  19. F font =New F ("", 12 );
  20. W. writeline (font. Name. tostring () + font. Size. tostring ());
  21. }
  22. // The preceding using statement is equivalent to the following precompiled statement.
  23. # If debug
  24. Textwriter W = file. createtext ("E: // test.txt ");
  25. Try
  26. {
  27. W. writeline (@"Using statement usage: The using statement allows programmers to specify when the resource object should be released. Resource management statement function ");
  28. }
  29. Finally
  30. {
  31. // Standard writing method. The following statement can also be directly written as W. Dispose ()
  32. If (W! = NULL) (idisposable) W). Dispose ();
  33. }
  34. # Endif
  35. // You can declare an object in the using statement or declare an object before the using statement, as follows:
  36. Textreader r = file. opentext ("E: // test.txt ");
  37. Using (r)
  38. {
  39. String stringd = R. readtoend ();
  40. Console. writeline (stringd );
  41. }
  42. }
  43. }
  44. }
// Using is used as the namespace indicator using system; using system. collections. generic; using system. text; using system. io; namespace statement {// specify the alias of the font class as f using F = system. drawing. font; Class program {static void main (string [] ARGs) {// usage of using statement to manage resources using (textwriter W = file. createtext ("E: // test.txt") {W. writeline (@ "using statement usage: The using statement allows the programmer to specify when the resource object should be released. Has the resource management statement function "); // uses the alias to instantiate the object F font = new F (" ", 12); W. writeline (font. name. tostring () + font. size. tostring ();} // The preceding using statement is equivalent to the following precompiled statement # If debug textwriter W = file. createtext ("E: // test.txt"); try {W. writeline (@ "using statement usage: The using statement allows the programmer to specify when the resource object should be released. Has the resource management statement function ");} finally {// standard writing method. The following statement can also be directly written as W. Dispose () if (W! = NULL) (idisposable) W ). dispose () ;}# endif // you can declare an object in the using statement or declare an object before the using statement, as follows: textreader r = file. opentext ("E: // test.txt"); Using (r) {string stringd = R. readtoend (); console. writeline (stringd );}}}}

 

My most common problems are:

Using (sqlconnection conn = new sqlconnection (sqlhelper. sqlconnection ))
{
Dataview DV = sqlhelper. executedataset (Conn, commandtype. Text, SQL). Tables [0]. defaultview;
}

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.