C # Programming Language Programming and Development Study Note 5: C # advanced features

Source: Internet
Author: User
Tags processing instruction

C # advanced features

1 Inheritance
1) The derived class inherits all the members of the base class except the constructor and destructor.
2) Unlike C ++, C # only supports a single inheritance of classes.
3) C # provides a keyword base to access base class members and call base class constructor.
4) The New Keyword can be used to overwrite the members of the base class.

2 polymorphism
1) C # define Virtual Methods in the base class by using the keyword virtual, and use override to overload Virtual Methods in the derived class to achieve polymorphism.

3 Abstract)
1) abstract classes are declared with abstract keywords. They can only be used as base classes and cannot be instantiated.
2) an abstract class can contain abstract members or non-Abstract members. If an abstract member is contained, it cannot be implemented by {} and cannot be accessed by base in a derived class. Abstract METHODS can only be used in abstract classes.
3) You cannot use the sealed keyword for the abstract class.
4) A non-abstract class derived from an abstract class must be overloaded to implement all the abstract members it inherits.

4 sealed)
1) The sealed class cannot be inherited. The sealed class cannot be an abstract class at the same time.
2) The purpose of the sealing method is to make it impossible to reload the method from the derived class of the method's class. Therefore, the sealing method must be an overload of the base class virtual method.

5 Operator Overloading
1) in C #, all operators belong to the static method of the class (public static ). All parameters must be listed in the parameter list (they cannot be used as parameters themselves)
2) In C ++, operators are not static methods of classes, and they themselves are the first parameter. Or, the operator is not a member of the class, but serves as the friend method. In this case, all parameters need to be listed, similar to C #.

6. type conversion
1) implicit type conversion: Low-precision to high-precision conversion (or integer to floating-point number conversion, the accuracy of the value of the decline unchanged); decimal 0 to any enumeration type conversion
2) Explicit type conversion: high-precision to low-precision, numeric to Enumeration type conversion, and explicit conversion using the convert class (an exception is thrown when the conversion fails ).
3) type reference conversion: conversion from a derived class type reference to a base class type reference. Convert a null or base class type reference to a derived class (Conversion failure throws an exception)
4) packing and unpacking: Packing: Value Type value -- copy --> object instance; disassembling: object instance -- copy --> value type value

7 Structure
1) structure to make a value type, class is a reference type
2) structures do not support inheritance (but interfaces can be implemented)
3) The default constructor cannot be declared in the structure.
4) because it is a value type, you can use the new or new keyword to create an instance.
5) C # The document points out that a class smaller than 16 bytes may be more efficient to process as a structure.

8 Interfaces
1) interfaces are combined with a series of related operations through classes or structures.
2) The default access method for interface members is public.
3) interface members can be methods, properties, index indicators or events, but not fields.
4) interfaces support multi-Inheritance
5) If a class is inherited from multiple interfaces and its interfaces have repeated method declarations, you can use explicit member Implementation of the interface. explicit implementation can only be called through the interface.

9 sets
1) A set of objects that can be accessed by traversing each element. We recommend that you use the related geometric classes provided by. Net to derive your own set.
2) if a set implements ienumerable, you must also implement ienumerator.
3) Comparison between array and arraylist:
(1) array can be defined as a multi-dimensional array, while arraylist can only be one-dimensional.
(2) array can define its own lower limit, while the lower limit of arraylist is always 0
(3) array elements are usually of a specific type, while arraylist elements are of the object type. Therefore, binning and unpacking are usually required when operating the arraylist elements, which affects the performance.
(4) The number of elements in array is fixed, and the number of elements in arraylist can be automatically expanded.
(5) array only provides operations on a single element, while arraylist also provides methods to operate on elements within a range.

10 Indexer
1) Indexer provides array-based access to classes.
2) It is generally written as follows: Public String This [int ind] {get {} set {}}

11 Exception Handling
1) three types of exceptions: systemexception; ioexception; applicationexception. User-defined exceptions must be derived from applicationexception.
2) exception throw and capture
Try
{
}
Catch ([expression])
{
}
Finally
{
}
Even if the Catch Block contains a break, continue, Goto, or return statement, the finally code will always be executed.
3) it can contain multiple catch statements, but when an exception occurs, only the first matching Catch Block is executed. Therefore, if the exception type is the same as the base class or type of the exception type, an error occurs.
4) When a catch statement is used to handle an exception, it can throw the exception again, and embed the original exception into the new exception so that you can know the entire exception throw process. The innerexception attribute of exception can be used to obtain the original exception.

12. Delegate
1) A delegate is a reference type. It uses a set of specific parameters and return types to encapsulate methods, similar to function pointers in C, but with security.
2) multicase delegate: A combination delegate can only combine the same type of delegate, and its return type must be void. It can be implemented using ++,-, ++ =,-=.

13 events
1) Subscriber can also be a publisher user who publishes an event. When publisher generates an event (delegated to be executed ), all users will be notified and provide responses to these events (the processing method associated with the delegate is automatically executed ).
2) The release event class defines the Events declared by the delegate, and defines the method for responding to the event in the user class. This method is associated with the event through the delegate.
3) declare an event as follows:
Public Delegate void eventhandler (Object sender, eventargs E );
Public event eventhandler onevent;

14 pre-processing commands
1) Unlike C ++, C # does not have an independent pre-processing instruction, and its pre-processing instruction is actually executed by the compiler.
2) one row can only have one preprocessing command.
3) # define/# UNDEF; # If/# Elif/# else/# endif; # warning/# error; # Line;/# region/# endregion

15 attribute)
1) before the class declaration and class member declaration, it is used to specify some additional information.
2) apply attributes to program entities. The additional information they specify can be used in the document description of the Program for explanatory work. reflection technology can be used to retrieve the information when the program is running.
3) attributes are defined through the Declaration of attribute classes and are derived from system. attributes.
4) use of the conditional and obsolete attributes.

16 components and assembly
1) DLL hell: when multiple applications try to share a public component (such as a dynamic connection library (DLL) or a Component Object Model (COM) Class) A series of problems. The most typical scenario is that an application will install a new version of the shared component, which is not backward compatible with the existing version on the machine. Although the newly installed application runs normally, applications that originally relied on the previous version of shared components may no longer work.
2) The Assembly includes: Assembly List, type metadata, msil code, and resources.
3) Assembly features: self-instruction (no need to query the registry or other information); parallel loading (different versions of the same program can be used in the system at the same time); no impact on installation (only need to copy)

 

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.