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
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.
1using System;
2using System.IO;
3 4publicclass Test
5{
6staticvoid Main()
7 {
8int num = Add(5, 6);
9 Console.WriteLine(num);
10 Console.ReadKey();
11 }
12publicstaticint Add(int x, int y)
13 {
14return 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
1using System;
2using System.IO;
3usingstatic System.Console;
4publicclass Test
5{
6staticvoid Main()
7 {
8int num = Add(5, 6);
9 WriteLine(num);
10 ReadKey();
11 }
12publicstaticint Add(int x, int y)
13 {
14return 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
.
1using System;
2namespace a
3{
4using System.IO;
5publicclass Test
6 {
7 8staticvoid Main()
9 {
10 Console.WriteLine("namespace a");
11 FileInfo file = new FileInfo("file path");
12 }
13 }
14}
15namespace b
16{
17publicclass Test
18 {
19staticvoid 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.
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.
文章评论