C # review ④,

Source: Internet
Author: User

C # review ④,

C # review ④

June 16, 2016

12: 37

Main Classes and Structs class and struct

1. Contents of Classes

Field, constant, method, constructor, destructor;

Features, events, indexers, and heavy-duty operators;

Nested type (class, interface, struct, enumeration type, delegate)

2. Classes Class

Stack class: array, stack pointer, constructor, pressure stack, and output Stack

3. Structs struct

Vertex struct: coordinate (x, y), constructor, and MoveTo Method

4. Visibility Modifiers Visibility Modifier

Public visible where the declaring namespace is known is visible under the current namespace

Private only visible in the declaring class or struct is only visible in the class or struct definition Field

The default fields of the class and struct are private, and the default method is public.

5. Fields and Constants

· In the class, field Initialization is optional during Declaration (Initialization is allowed. If Initialization is not performed, the compiler will perform the default initialization );

· Constants must be initialized during Declaration; otherwise, an error will be reported during compilation;

· The Readonly field must be initialized during Declaration or in the constructor; the value cannot be involved in calculation during compilation; the value cannot be changed afterwards; the occupied memory space

6. static fields and constants

Static fields belong to the class and do not belong to the Instance Object (when the class does not instantiate any object, static fields have been generated, and static fields are fixed attributes of this class, all objects that can be constructed through this class are shared)

Constants cannot be declared as static.

Static readonly requires note that for a Reference type of static readonly, the value assignment (write) operation is not allowed. The read and write operations on its members are still unrestricted.

7. Methods, functions, and processes

Method: there may be a return value or no return value

Process: No Return Value

Function: Return Value

8. Static Methods Static method

Static method to process static Fields

9. Parameters

Two parameter passing Methods: Value passing and reference passing

When using Ref parameters, the input parameters must be initialized first. But Out is not required. For Out, initialization must be completed in the method.

When using Ref and Out, you must note that the Ref or Out keyword must be added to both the method parameters and the execution method to match.

Out is more suitable for the place where multiple Return values are to be returned, while Ref is used to modify the method used for calling Out the user's reference.

Ref modifier: The parameter can be input/output

Out modifier: the parameter is output-only.

10. Variable Number of Parameters Variable array Parameters

C # syntax stipulates that params must be followed by arrays.

The function is to load an indefinite number of parameters of the same type into this array.

String is also a subclass of the object, so you can use an object [] array instead of string []

11. Method Overloading

Method overload: Two or more methods in a class have the same function name.

The method is overloaded if the parameter is different, the parameter type is different, or the Parameter definition (out/ref) is different.

Overload does not require the function to return different types, that is, when two functions only return different types, they do not meet the overload conditions!

Conditional overloading:

12. Constructors for Classes Class Constructor

Class constructor has a visibility modifier, no return value, and the function name is the same as the class name;

If you do not add constructor when declaring a class, the compiler automatically adds a default constructor during compilation, and the constructor has no parameters.

If you manually add a constructor when declaring a class, the compiler will not add a non-argument constructor for the class, that is, an error will be reported when you manually call the non-argument constructor during Object Instantiation.

A class can have multiple constructor functions. These constructor functions do not return values and have the same name. According to the previous definition of overload, all constructors in a class are overload functions.

For constructors, you can choose to initialize fields. When not initialized, the default constructor initializes each field according to the following rules.

13. Constructor for Structs Constructor

The constructor of the struct has a visibility modifier and no return value. The constructor name is the same as the struct name;

Each struct has a default no-argument constructor;

Programmers cannot create a constructor with parameters for struct;

The constructor of the struct must Initialize all fields.

14. Static Constructors Static Constructor

The static constructor must have no parameters and no visibility modifier;

A class or struct has only one static constructor;

Static constructors are used to initialize static fields;

When this type is used for the first time, the static constructor is called immediately.

15. Destructors destructor

Call the Destructor before an instance object is reclaimed;

The Destructor has no visibility modifier;

Struct has no destructor;

16. Indexers Indexer

The indexer includes the visibility modifier, return value, function name, and index type.

17. Operator Overloading

To overload an operator, you must reload the corresponding operator after it is overloaded. For example, if the addition operator is reloaded, the subtraction operator must also be reloaded;

Operators that can be overloaded:

Mathematical operators: +,-, *,/, %, ++, and ,--

Relational operators: = ,! =, <,>, <=,> =

Bitwise operators: &, |, ^

Other operators :! ,~ , >>, <<, True, false

The overload operator must return a function value;

18. Conversion Operators type Conversion overload

Explicit type conversion (Explicit conversion)

Implicit conversions Implicit conversion

Recommended: https://msdn.microsoft.com/en-us/library/09479473 (v = VS.80). aspx

Class SampleClass
{
Public static explicit operator SampleClass (int I)
{
SampleClass temp = new SampleClass ();
// Code to convert from int to SampleClass...

Return temp;
}
}

19. Nested TypesNested type

The internal class can access all the member variables and member functions of the external class;

The external class can access the member variables and member functions that are visible as public in the internal class;

Other classes can be accessed only when the internal class is public;

To access the internal class, add the dot operator "." And the internal class name to the external class name.

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.