3. Step by Step C # (3): objects and types

Source: Internet
Author: User

Class and Structure

Classes and structures are actually templates for creating objects. Each object contains data and provides methods to process and access data ,.

The difference between structures and classes is that they are stored in the memory. The access method (the class is the reference type stored on the heap), while the structure is stored on the stack (stack) and some of their features (for example, the structure does not support inheritance ). The structure is very similar to the class. The main difference is to use the keyword struct instead of class to declare the structure.

For classes and structures, use the keyword new to declare the instance: This keyword creates an object and initializes it.

The Field Values of the class and structure are both 0 by default:

The data and functions in a class are called class members.

Data members: data members are members of fields, constants, and events that contain Class data. The data member can be static data.

Fields are class-related variables.

The association between constants and classes is the same as that between variables and classes. The const keyword declares a constant. If it is declared as public, it can be accessed outside the class:

Function Member

Function members provide certain functions of data in the operation class, including methods, attributes, constructor and terminator (finalizer), computation, and indexer.

Method

The formal C # Glossary functions and methods.

Method Declaration

Indicates the. NET base class system. Drawing. rectangle of the rectangle.

If the method has a return value, the return statement must be used together with the return value. If the method does not return a value, specify the return type as void, because the return type cannot be omitted.

Method can contain any number of return statements

Call method.

Mathtest describes the definition and instantiation of classes, the definition of methods, and the syntax of calling. In addition to the class containing the main () method, math test is also defined.

The Code shows that the mathtest class contains a field and a method. This field contains a number. This method calculates the square of the number. The class also includes two static methods, one returns the PI value, and the other calculates the square of the number passed in as the parameter.

The following example shows parametertest. CS indicates that if the array or other reference types (such as classes) are passed to the method, the corresponding method will use this reference to change the value in this array, the new value is reflected in the original array object:

Note that the value of I remains unchanged, while the value changed in ints also changes in the original array.

Out Parameter

The compiler uses the out keyword for initialization. When the out prefix is added before the input parameter of the method, the variables passed to the method can not be initialized. the variables are passed through references, therefore, any changes made to the variable in the method returned by the method will be retained. The out keyword is also required when the method is called, same as defining this method:

The naming parameters can be passed in any order as follows:

If the method has several parameters, you can mix location and name parameters in the same call.

Optional parameters can also be optional. Optional parameters must also be the last parameter of the method definition:

To work properly, you must define the optionalnumber parameter at the end.

Method overload. To overload the method, you must declare the number of parameters in the same list or the method with different parameter types:

If you cannot use optional parameters, you can use method overload to achieve this goal:

The two methods cannot be different only in the return type. The two methods cannot be distinguished only based on whether the parameter is declared as ref or out.

Attribute

It is a method or a pair of methods. In the client code, it is a field.

C # defines attributes. You can use the following syntax:

 

Constructor

The syntax for declaring a basic constructor is to declare a method with the same name as the class, but this method does not return a type;

The reload cycle of the constructor is the same as that of other methods.

The compiler does not automatically provide the default constructor for constructors with parameters.

}

}

The constructor is defined as private or protectcd, so irrelevant classes cannot access them:

Compile a common static attribute in mynumber or the class has been instantiated by the method. Two useful cases: A class is only used as a container for some static members or attributes, so it will never be instantiated. You want the class to be instantiated only by calling a static member function.

Static Constructor

The reason for writing a static constructor is that the class has some static fields or attributes. You need to initialize these static fields or attributes from the external source before using the class for the first time. Note that the static constructor does not have a method modifier. The static constructor does not contain any parameters. A class can only have one static constructor.

The above example describes the usage of static constructor. To use the color structure, you must reference the system. Drawing. dll assembly and add a using statement to the system. Drawing namespace;
Using system;

Using system. drawing;

Use the following code to test the static constructor:

Run the code and get the following results:

Anonymous type

VaR keyword, which is used to indicate implicitly typed variables. When using var and new together, you can create an anonymous type. The anonymous type is only a class inherited from the object and has no name.

If an object needs to contain a person's last name, intermediate name, and first name, the statement is as follows:

Structure

The constructor and attribute of the structure are described in the following code:

}

}

The structure is a value type, not a reference type ,. The structure does not support inheritance. There are some differences in the working methods of the structure constructor. In particular, the compiler always provides a default constructor without parameters, which cannot be replaced.

Constructor

The default constructor initializes All numeric fields to 0, and initializes the reference type field to null, which is always provided implicitly. The provided field initialization value cannot bypass the default constructor. The following code produces a compilation error:

Classification

Usage of the partial Keyword: Place partial before the class, struct, or interface keyword, for example:

In the nested type, as long as the partial keyword is located before the class keyword, You can nest the partial classification.

When a partial category is compiled to a type, the attributes, XML comments, interfaces, and parameter attributes of the generic type are merged.

Static class

If a class only contains static methods and attributes, the class is static.

Tostring () is the most convenient way to quickly obtain the string representation of an object.

Example;

}

}

This example shows that if tostring () is not rewritten in a defined class, the class inherits system. the implementation method of the object. If you want tostring () to return a string, it contains the value information of the object in the class. This example also describes how to use Attribute encapsulation fields.

 

3. Step by Step C # (3): objects and types

Related Article

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.