C# Methods and Parameters: Common Namespace Summary, Usage of 'using', Main Method Parameters

2019年12月9日 2659点热度 0人点赞 0条评论
内容目录

This article mainly discusses

  • Common C# namespaces
  • using static directive && calling static methods
  • Nested namespaces && scope
  • Alias
  • Main() method

Common C# namespaces

 

Namespace Function
System

Defaults for file creation, handling mathematical computations, environment variables, console output, garbage collection, and common exceptions and features.

System.Collections

Same: both handle collections of objects (lists, dictionaries, etc.)

Different: the latter uses  generic strongly typed collections

System.Collections.Generic 
System.Data

Used for database handling

For example, ADO.NET

System.Data.Odbc

System.Data.Oracle

Client System.Data.OleDb

System.Data.SqlClient

System.IO 

Handles file input/output (I/O)

Data compression

Port operations

System.IO.Comoression 

System.IO.Ports 

System.Drawing

Used for drawing and image processing

System.Windows.Froms Contains types for building user interfaces and various controls
System.Windows 

WPF UI toolkit

System.Windows.Controlls
System.Windows.Shapes
System.Linq 

Provides LINQ functionality (Language Integrated Query)

 

System.Web

For building .NET Web applications, supporting ASP.NET

System.ServiceModel 

For building distributed applications via WCF API

System.Workflow.Runtime

Types for building workflow-supporting applications using WCF API

System.Workflow.Activities
System.Threading

Types for building multithreaded applications

System.Threading.Tasks

 Task-based asynchronous operations

System.Security 

A security-focused namespace with many types for handling permissions, encryption, and other issues

System.Xml

Types for XML data interaction

 

Note: In C#, when introducing a namespace, its subnamespaces cannot be used.

For example, if System.Collections is imported, classes and methods within System.Collections.Generic cannot be used.

 

For more different functional and categorized namespaces, it is recommended to check the article

https://blog.csdn.net/lidandan2016/article/details/77994232?locationNum=8&fps=1


using static directive && calling static methods

In C#, classes from different namespaces need to be imported first, and then instantiated with new before they can be used. For static classes, they can be used directly after the namespace is imported.

 1 using System;
 2 using System.IO;
 3 
 4 public class Test
 5 {
 6     static void Main()
 7     {
 8         int num = Add(5, 6);
 9         Console.WriteLine(num);
10         Console.ReadKey();
11     }
12     public static int Add(int x, int y)
13     {
14         return x + y;
15     }
16 }

For example, the method used to output strings to the console Console.WriteLine() can be used directly after importing System, as seen in line 9 of the above code.

Style    Class.Method();   Console.WriteLine()

For methods in the same file as Main, they must be static methods and can be called directly by their method name. As shown in line 8 of the above code.

Style  Method();  For example, Add();

 

If I want to use a static method that is not in the same namespace directly with Method(); rather than Class.Method(), what should I do?

This requires the use of the using static directive.

The using static directive specifies a type so that its static members and nested types can be accessed without specifying the type name.

For example

using static System.Console;

Modify the previous example code, paying attention to lines 9 and 10

 1 using System;
 2 using System.IO;
 3 using static System.Console;
 4 public class Test
 5 {
 6     static void Main()
 7     {
 8         int num = Add(5, 6);
 9         WriteLine(num);
10         ReadKey();
11     }
12     public static int Add(int x, int y)
13     {
14         return x + y;
15     }
16 }

Now you can directly use WriteLine() .

Note:

  • This method, while simplifying length, may lead to many issues such as name conflicts and reduced readability.
  • Using static only works for static types!

 

Nested namespaces && scope

Namespaces also have scope; types outside of the scope cannot be used. However, importing a namespace is not as "flexible" as variable declarations.

Importing a namespace is done using the using directive, which can be used outside of a namespace or at the top inside a namespace and outside of a class.

The scope of using directives applied outside a namespace is  all namespaces in that file.

The scope of using directives applied inside a namespace is only within that namespace.

Here’s an example

.

 1 using System;
 2 namespace a
 3 {
 4     using System.IO;
 5     public class Test
 6     {
 7         
 8         static void Main()
 9         {
10             Console.WriteLine("namespace a");
11             FileInfo file = new FileInfo("file path");
12         }
13     }
14 }
15 namespace b
16 {
17     public class Test
18     {
19         static void Main()
20         {
21             Console.WriteLine("namespace b");
22             FileInfo file = new FileInfo("file path");    //error
23         }
24     }
25 }

 

In the first line, `using System;` can be used in both namespace a and b, however, only namespace a has `using System.IO;` (in the fourth line), namespace b cannot use it.

 

Note:

It's rare to write code this way because convention dictates that each file should have only one type declaration and import namespaces at the top.


Alias

In C#, you can use the `using` directive to create an alias for a namespace or type, and the alias will take effect throughout the entire file.

The most common use is to eliminate ambiguity between two types with the same name and to abbreviate names.

Usage:

using alias = namespace or type;

Example (second line):

 1 using System;
 2 using Test1 = System.Console;
 3 namespace core
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Test1.WriteLine("Test");
10         }
11     }
12 }

 


 

Main Method

The Main method is the entry point of a program and generally has the following four forms:

 

  • No parameters, return type is void
    static void Main()
  • No parameters, return type is int
    static int Main()
  • With parameters, return type is void
    static void Main(string[] args)
  • With parameters, return type is int
    static int Main(string[] args)
    

      

The considerations for these forms involve providing command-line arguments when executing the program and returning a status identifier in the Main method.

When developing C# using Visual Studio, these forms are generally not needed. However, when manually compiling source code for console applications, the provided arguments will be passed to the Main method.

A C# program can contain multiple Main() methods, but at runtime, one Main must be determined or specified as the entry point.


 

痴者工良

高级程序员劝退师

文章评论