C # Learning Note 2

Source: Internet
Author: User
Tags stack trace

1. namespace declaration: A namespace is a special classification mechanism that groups all types that are related to a particular feature. In general, the outer namespace is designated as the company name, followed by the product name, and finally the functional area, such as Microsoft.Win32.Networking. Namespaces can contain a period, which makes the namespace "seem" hierarchical, but it is only advantageous for readability because the compiler considers all namespaces to be on the same level brush.

For example, System.Collections.Generics appears to be contained within the System.Collections namespace, but for compilers. They are two completed, completely different namespaces.

2.the difference between using imports: the difference between placing a using directive at the top of a file and placing a using directive at the top of the namespace declaration is that the using directive of the latter is valid only within the namespace that you declare, if The outside of the Awl.Mlchaelis.EssentialCSharp namespace declares a new namespace, and the new namespace is not subject to the using. The effect of the system directive. However, it is very rare to write code like this, especially according to the Convention, where each file should have only one type declaration.

3.using aliases: Use a using directive to take an alias for a namespace or type, which is an alternate name that can be used within the scope of the using directive. The two most common reasons for using aliases are to disambiguate and abbreviate a long name with two types of the same name.

4. Procedure Entry method: The Main () method supports the int return value instead of just supporting Vold return. The return value is optional for the main () declaration. But if it is used. The program can return a status code to the caller (the caller can be a script or a batch file). By convention, a non-zero return value represents an error. Although all command-line arguments can be passed to main () through a string array, we may sometimes need to access those parameters from a method different from main (), in which case you can use the System.Environment.GetCommandLineArgs () method, which takes the same way as main (string[] args) to pass arguments to main () to return command-line arguments.

5.c#4.0 added:

(1) Optional parameters: Optional parameters must be placed after all required parameters (parameters without default values). In addition, the default value must be a constant, or it must be a value that can be determined at compile time, which greatly limits the "optional parameters" application.

such as: Directorycountlines (String directory,string extension= "*.cs")

(2) named parameter: With named parameters, the caller can specify the parameter name explicitly. and assigning a value to the parameter, instead of just deciding which value to assign to the parameter in order of the parameter, as before,

such as: Displaygreeting (firstName: "Lucy", LastName: "Jack");

public void displaygreeting (String firstname,string middlename=default (String), String Lastname=default (String))

{

.... Omitted

}

If a side station has a large number of parameters, and many of them are optional (this is a very common case when accessing a Microsoft COM library), then the naming parameter syntax can certainly bring a lot of convenience. However, it is important to note that the cost of this convenience is to sacrifice the flexibility of the method interface, in the past {at least in C #), the parameter name can be freely changed without causing the calling code not to compile. However, after you add a named parameter, the parameter name becomes part of the method interface, and changing the name causes the code that uses the named parameter to fail to compile.

6. If you use methods such as method overloading, optional parameters, and named parameters, it is difficult to see exactly which method is the final call, except that all parameters (with the exception of optional arguments) have exactly one corresponding argument (whether by name or location). And if the argument has a compatible type, then a call is applied to (compatible with) a method. Although this limits the number of methods that can be called, but is not sufficient to uniquely identify the method, the compiler uses only the parameters that are explicitly identified by the caller, ignoring all optional parameters not specified by the caller. So, if a method has an optional parameter that causes two methods to be applied, the compiler will eventually choose a method with no optional parameters.

When the compiler chooses the method of the final call from a series of "applicable" methods, it is based on which method is most specific and only one method exactly matches the parameters passed by the caller, so the method always has the highest priority.

For example, if the caller is passing an int, the method accepting the double will take precedence over the method that accepts the object, because double is more specific than object, and if there are multiple applicable methods that cannot be selected from the most unique, the compiler will give an error indicating that the call is ambiguous. When writing the order, it is best to use explicit transformations to make it easier for others to understand which target method you want to invoke.

7.use of try-catch-finally: Catch is specific to general, if after capturing the catch block of exception to add a specific catch block, the compiler will report a warning message, Indicates that the specific catch block will never be executed. Catch blocks that do not specify a data type are called generalized catch blocks (generic catch block), which is equivalent to getting a catch block of the object data type.

For example catch (Object exception) {...), because all classes are ultimately derived from object, the catch block without a data type must appear at the end. A generalized catch block is seldom used because there is no way to catch any information about the exception. Avoid using exception handling to handle expected situations, and developers should try to avoid throwing exceptions for expected situations or normal control flows, which are specifically designed to track exceptions, unforeseen, and potentially serious consequences, using exceptions for expected situations, Will cause your code to be difficult to read, understand and maintain.

C # has a slight performance penalty when throwing an exception, and most operations are nanosecond-level, which can cause millisecond latency, which is not normally noticed unless the exception is not handled.

8.Throw in Catch block: You can re-throw an exception in a catch block (such as in a catch (ArgumentNullException ex) implementation, you can include a call to the throw ex), and then re-throw the exception like this , the stack trace is reset to the re-raised position instead of reusing the original raised position. Inside a catch block, if you re-throw a different exception, not only will the throw point be reset, but the original exception will be hidden, and in order to preserve the original exception, you need to set the InnerException property of the new exception (which can usually be assigned by the constructor).

9. Custom Exception: The only hard requirement for a custom exception is that it must derive from System.Exception or one of its subclasses, and in addition, the following best practices should be followed when using custom exceptions.

(1) All exceptions should use the "Exception" suffix to demonstrate their usefulness.

(2) Typically, all exceptions should contain the following 3 constructors: A parameterless constructor, a constructor that gets a string argument, and a constructor that gets both a string and an inner exception as a parameter.

(3) Avoid the use of deep inheritance hierarchies (which should generally be less than level 5).

starting with the net Framework 4, an enumeration type also adds a TryParse method.

-------------------------------------------the above content according to the "C # Essence of the third edition" to Organize

C # Learning Note 2

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.