3. Learn c # (3): objects and types,

Source: Internet
Author: User

3. Learn c # (3): objects and types,

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.

 


Want to learn C language, some problems

1. To what extent do you want to write simple software? If you have been learning C for about a year (normally), you can write some simple personnel management and other database-related small programs. It cannot be called software.
2. The preferred C language programming design by Tan haoqiang. The university is an inspiration for this book, which is very detailed. Then, let's take a look at the C programmer's book and learn the Important Algorithms in it.
3. Not recommended
4. It can be said that it is all about mathematics, but it is different from the math used in the examination. It focuses on many aspects.
5. Java, but after learning C, Java is more studious and Java is more practical than C. Therefore, after learning C, we suggest learning Java.

PS: I am a computer student. If you have any questions, leave me a message or give me a Hi. I hope it will help you.
Q: 617899504

How to Learn C Language

Answer your question:

1. First, read a book, read a book, and tap on the Code (it doesn't matter if you don't understand it ),
Read books-> code-> read books-> code-

2. C language entry classic, data structure, algorithm Guide Wheel, algorithm Introduction

3. I know what C language is. Now I can win a prize in the acm/icpc competition (college Program Design Competition). If I want to learn it well, I will certainly learn it well.

4. Don't look at this at the beginning. Wait until you get started and watch it again. If you want to become a master, you must watch it.

The best way to learn C is to practice more. Don't just look at it. You can only grasp it with more clicks on the keyboard.
I feel that I want to learn the C language: Read more books and write more code.

You can go to the judge on the Internet to do the exercises (exercise the algorithm). Many universities have:
Peking University: acm.pku.edu.cn/JudgeOnline/
Kyrgyzstan: acm.jlu.edu.cn/joj/
A lot more.
If you only want to learn the basics of C language and do not want to study algorithms, you don't need to read algorithm guide wheel or algorithm introduction. You can also get started with General C language books.

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.