C # basic review 1

Source: Internet
Author: User

1. Classes and objects


1.1 concepts of classes and objects
Class: the object type, which is different from the basic data types such as int, because the class has behavior; it can also be said that
A set of objects with the same features and behaviors.
Objects: objects are objects that you can see and touch. They are real objects with the same features and
Behavior objects can be abstracted into classes.
1.2 Definition class syntax
[Modifier] class classname
{
// [Field, method]
}
1.3 How to create an object
Classname name = new classname ();
1.4 namespace:
Role: used for management;
Declare namespace;
Import using.

2. Encapsulation


2.1 encapsulation Concept
Encapsulation refers to packaging things together and then presenting them in a new and complete form. For object-oriented, encapsulation
It is to package methods and fields into a program unit, and these units are implemented in the form of classes. So let's say
The encapsulation is to privatize fields and provide public methods to access private fields.
Is the class that implements the encapsulation feature.
Encapsulation is one of the important concepts of object-oriented programming. You can hide the implementation details of a class through encapsulation.
The properties and methods of relatively public access communicate with the outside world.
2.2 attribute syntax
Access modifier type attribute name
{
Property accessors (including set and get)

}
Set accessors are responsible for setting Field Values
The get accessor obtains the field value.

2.3 Constructor
Same as the class name;
No response type. You do not need to identify void;
It can be neither a parameter nor a parameter;
Purpose: Initialize the object.
2.4 method overload
1. the overload method has the same name and is in the same class.
2. the type or number of parameters of the overload method is different.

3. Inheritance:
3.1 concept of Inheritance
One of the three main features of object-oriented, inheritance is the relationship between two classes: the Child class inherits the parent class, and naturally has
Features of the parent class (except constructors, static members, and private members ).
3.2 inherited Value
Inheritance not only achieves code reuse, but also simulates the relationship between the real world. In object-oriented programming
Split all objects. Inheritance simulates the relationship between objects. In addition, inheritance clears the hierarchy of parent classes and child classes.
The subclass only needs to focus on its own status and behavior. The status and behavior of the parent class are handed over to the parent class for processing.
3.3 inherited syntax
When declaring a subclass, add a colon to the right of the class name, and write the class name of the parent class on the right of the colon.
3.4 inherited features
1. single sex 2. transmission.
3.5 inheritance and constructor
When a subclass constructor is called, the constructor of the parent class is called first.
3.6 base usage
1. Use base () to call the parent class constructor.
2. Use base to call parent class members.

3.7 Lishi replacement principle
A simple explanation is that child class objects can replace parent class objects.
Keyword is and as: use is to determine the object type and use as to perform type conversion.

4. Polymorphism
4.1 concept of Polymorphism
Polymorphism is one of the three main characteristics of the class. Encapsulation, inheritance, and polymorphism are interrelated. Polymorphism exists
In inheritance, it is a further extension of inheritance. There is no polymorphism without inheritance. Polymorphism can be used to achieve
Code reuse reduces the amount of code and improves code scalability and maintainability. Implementation Method Based on inheritance
Rewriting is the key to implementing polymorphism.
4.2 Use of Polymorphism
Use different instances to perform different operations for the same reference type.
1. Use the parent class as the method parameter.
2. Use the parent class as the return value type of the method.
4.3 virtual method: the virtual modifier.
4.4 rewrite method: the override modifier is used.
4.5 requirements for method Rewriting
1. The same method name.
2. The same parameter list.
3. the return value type must be the same or a subclass.
4. The access permission of the method to be overwritten cannot be reduced.
4.6 differences between the hidden method and the override method:
(1) The keywords are new and override.
(2) The base class method is hidden and overwritten.
(3) polymorphism can be implemented based on rewriting, but not the hidden method.
4.7 upward transformation Syntax:
<Parent type> <reference variable name> = new <child type> ();
Downward transformation Syntax:
<Child type> <reference variable name >=( <child type>) <reference variable of the parent type>;

5. abstract classes and interfaces
5.1 Definitions of abstract classes and abstract methods
Public abstract class name
{
Public abstract void method name ([parameter list]);
}
5.2 differences between abstract and Virtual Methods
(1) The keywords are abstract and virtual.
(2) abstract methods must be in abstract classes. Virtual methods can be in common classes or abstract classes.
(3) abstract methods can only be declared by methods, but cannot be implemented. Virtual methods can be implemented.
(4) abstract methods must be overwritten for non-abstract classes that inherit abstract classes, while virtual methods can not be overwritten.
5.3 define the interface syntax
Public interface Interface Name
{
// Interface member (methods, attributes, etc)
}
Note the following points about the interface Syntax:
1) the interface name usually starts with "I", followed by the same part and class naming rules, that is, Pascal naming
Rule.
2) the methods in the interface do not specify specific implementations, that is, they are also abstract. For example, the showname () method here,
Different from abstract methods in abstract classes, abstract keywords cannot be used for modification.
3) the interface member is implicitly public and cannot display the specified access level. For example, the showname () method is public,
However, it cannot be specified as public.
5.4 benefits of abstract class interfaces
1. abstract classes are easy to reuse.
2. Interfaces facilitate code maintenance.
5.5 object-oriented design principles
1. Multi-Purpose Combination, less inheritance.
2. Programming for interfaces.
3. It is open for expansion and closed for changes.


6. Static and sealed
6.1 static members
Static members of a class include static methods, fields, and attributes.
Static members are declared with the "static" keyword.
Use the "class name. member name" method.
6.2 static method
Static methods can only access static members in this class, but cannot directly access instance members in this class.
6.3 instance method
The instance method can access both instance members and static members.
6.4 Static Field Usage
1. record the number of instantiated objects.
2. Stored values that must be shared among all instances.
6.5 static class
1. Only static members are included.
2. Objects cannot be instantiated.
3. Subclass cannot be derived.
6.6 Seal
Sealed class cannot be a subclass, but it can be instantiated.
6.7 Sealing Method
The method of rewriting is modified with the sealed keyword. This method cannot be rewritten by the quilt class.


7. Value Type and reference type
7.1 value type and reference type
The value type is generally stored in the stack, and the reference type is generally stored in the heap.
The Value Type stores the value directly, and the reference type directly stores the address or reference of the object.
7.2 syntax for defining structures
Access modifier struct structure name
{

Structure Member
}
7.3 syntax for defining Enumeration
Access modifier Enum enumeration name
{
Member 1, member 2 ,...... Member n
}
7.4 differences between ref and out
The variable corresponding to ref must have an initial value, and the variable may not have an out value.


7.5 Constants
It cannot be modified, it must have an initial value, it must be defined in the class, and the keyword Const.
7.5 packing and unpacking
Packing: Convert the value type to the reference type.
Binning: converts a reference type to a value type.


7.6 object and valuetype
Directly inheriting objects is a reference type, and inheriting valuetype is a value type

 

 

 

 

 

 

 

 

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.