C # advanced programming (Fourth Edition)-Reading Notes (1)

Source: Internet
Author: User
Tags protected constructor
I just started to learn C # And. NET and wrote some code. I feel that the basics are still very important. I want to learn C # advanced programming (Fourth Edition) carefully. With a thorough attitude, I will do some demos to verify what I don't know and what I feel is prone to errors. There will certainly be something wrong with your understanding. Please put it forward and make progress together. (The code in this article is all debugged in vs2005 .)

Time:

Content: Chapter 2 C #Basic (23 ~ 99)

1. Any variables (Value Type and reference type) defined in C # Must be initialized before use. Otherwise, an error occurs during compilation.

2. Pay attention to the special nature of the string reference type: although the string is a reference type, if you modify a string instance, a new string instance will be created, this is different from other reference types.

Iii. XML Document Description: C # automatically creates a document description in XML format based on specific annotations, which is represented by a three-slash ("").

Time:2008-2-26

Content: Chapter 3Object and type (100 ~ 116)

 

V,A constant member in a class cannot be declared as static. Is it static by default? Does a constant member belong to the data of the class or the instance of the class-the data of the object?

A constant member belongs to the data of a class. Because a constant is declared in a class, it is immutable after initialization. It is commonly used by a class. Similar to static members.

6. Three Methods for passing method parameters:

1. Value Transfer

2. The ref parameter is passed for reference and can be modified.

3. Output transmission and out are mainly output problems (multiple parameters are returned ).

C # requirements for passing method parameters: before passing to a method, whether it is passing by value or by reference, all variables must be initialized. (any modification to the value-passing parameter in the method will not be taken out of the method. Modifications to the ref parameter in the method will be taken out of the method to affect the real parameter, but such modifications can or will not happen ). Instead of initializing the variables passed to the method by passing out. However, the out parameter must be assigned a value in this method (this operation is required even if the out parameter has been initialized before it is passed to the method ).

7. How does one overload )?

1. There is no overload keyword. Method overloading involves the number of parameters and parameter types.

2. The two methods cannot be different only in the return type.

3. The two methods cannot be distinguished only by declaring the parameter as ref or out.

8. Differences between overload and override

1. Different functions: Method overload provides several possible implementation methods for one method in the same class, while method overwrite exists in the inheritance class, to implement different implementations of the same method as the parent class.

2. Different implementation methods: There is no overload keyword, and different methods are differentiated through the parameter list.

Heavy Load overload features (two must be one)
Public bool withdraw (double AMT, string name)
Public double withdraw (double AMT)
1. The method name must be the same
2. The parameter list must be different.
3. the return value types can be different.

Keyword used: override
Override features ):
Public override bool withdraw (...)
1. The method name is the same
2. The parameter list is the same
3. the return value type is the same.

9. Net Program compilation and execution process

1. Write a program

2 debugging, compilation passed,. Net code will be compiled into msil (MS intermediate language)

3. Then, perform normal JIT (Just In Time) Compilation during the running period to obtain the internal executable code.
10. constructor and initialization

0 constructor can be defined as private or protected

1 C # require that all variables or objects must be initialized before use (Note: errors are easy !). Most constructors perform initialization. C # does not have to write constructor for each Class. Generally, if no constructor is provided, the compiler will create a default constructor in the background. It only initializes all member fields as the standard default values (for example, the reference type is null, the numeric data type is 0, and the bool value is false ).

2. If no public or protected constructor is defined in a class, you cannot use the new method to create an instance of this class. This is useful in the following two cases:

A. The class closely includes static members or attributes, so it will never be instantiated.

B. You want the class to be instantiated only by calling a static member function.

For example:

Namespace inforexchangflat

{

Public class classsqldb

{

// Define a static instance

Private Static classsqldb instance;

// Define the sqlconnection object

Public sqlconnection objconnection = new sqlconnection ();

/// <Summary>

/// Static instance initialization Function

/// </Summary>

Static public classsqldb getinstance ()

{

If (instance = NULL)

{

Instance = new classsqldb ();

}

Return instance;

}

}

}

11 Special Constructors

1 static constructor:

A new feature of C # Is that you can write a static constructor without parameters for the class. This constructor is only executed once (usually executed before the class member is called for the first time ). The normal constructor will execute as long as the class object is created. Main role: Initialize static fields and attributes in the class. The static constructor does not have an access modifier.

For example:

Using system;

Using system. drawing;

Namespace DHCC. changjia

{

Public class userpreferences

{

Public static readonly color backcolor;

Static userpreferences ()

{

Datetime now = datetime. now;

If (now. dayofweek = dayofweek. Saturday

| Now. dayofweek = dayofweek. Sunday)

Backcolor = color. Green;

Else

Backcolor = color. Red;

}

Private userpreferences ()

{

}

}

}

2. Call constructors from other constructors

A class has multiple constructors, and each constructor needs to initialize some fields. All repeated code that will have a certain amount can be written to a constructor.

For example:

Class car

{

Private string description;

Private uint nwheels;

Public Car (string model, uint nwheels)

{

This. Description = model;

This. nwheels = nwheels;

}

Public Car (String Model): This (model, 4)

{

}

}

Differences between constants and read-only fields

Constant (const): A known value that cannot be modified. Const is static, but cannot be modified using static.

Read-Only field (readonly): it cannot be modified, but its value is unknown before running. It is assigned a value during running. The read-only field has a specific value assignment area: the constructor can assign values to read-only fields after calculation in the constructor. the read-only field can be a static field (a class has only one value ), it can also be an instance field (each instance has its own value)

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.