C # and OOP knowledge summary

Source: Internet
Author: User

Part I: Definitions of classes and objects 1 classes and objects

  A class is a reflection of an entity in the real world or the mind world in a computer that encapsulates data and the operations on that data.

  An object is a variable that has a class type.

2 relationship of classes and objects

An object is an instance of a class that is a template for an object.

Advantages of 3-Face image objects

1. Easy Maintenance
Object-oriented design of the structure, high readability, due to the existence of inheritance, even if the need to change, then maintenance is only in the local module, so maintenance is very convenient and low cost.
2. High quality
At design time, you can reuse existing classes that have been tested in the field of previous projects to make the system meet business requirements and have high quality.
3. High efficiency
In software development, according to the needs of the design of the real world of things to abstract, produce classes. Using this method to solve the problem, close to the daily life and the natural way of thinking, is bound to improve the efficiency and quality of software development.
4. Easy to expand
Due to the characteristics of inheritance, encapsulation and polymorphism, the system structure of high cohesion and low coupling is designed naturally, which makes the system more flexible, easier to expand and less cost.

4 Creating objects

New operator in C #, the New keyword can be used as an operator, modifier, or constraint.

New operator
Used to create objects and call constructors.

For example:
Class1 o = new Class1 ();
The new operator is also used to invoke the default constructor of a value type. For example: int i = new int (); <=> int i=0;

New modifier

In
When used as a modifier, the new keyword can explicitly hide members that inherit from a base class. Hiding an inherited member means that the derived version of the member replaces the base class version. Without using the new
Modifier, a hidden member is allowed, but a warning is generated. Using the new
Explicitly hiding a member cancels this warning and records the fact that it is replaced with a derived version. To hide an inherited member, declare the member in the derived class with the same name, and use the new
Modifier to decorate the member.

The difference between a member variable and a local variable

Define Location Differences:

Member variables: defined in the class, outside of the method.

Local variables: Defined in a method, or on a method declaration.

Differences in initialization values:

Member variables: All have default initial values.

Local variables: There is no default initial value, you must first assign a value if you want to use it.

Storage location Differences:

Member variables: stored in the heap.

Local variables: stored in the stack.

Life cycle Differences

Member variable: exists as the object is created. Disappears as the object disappears

Local variables: exist with the invocation of the direction and disappear as the method's call is complete. More strictly, when the scope of a local variable ends, it is destroyed

Value types and reference types

(1) Value type.

All value types in C # are implicitly derived from System.ValueType:

struct: struct (directly derived from System.ValueType).

Numeric type:

Integral type, SByte (alias of System.SByte), short (system.int16), int (System.Int32), Long (System.Int64), Byte (System.Byte), ushort (system.uint16), u Int (system.uint32), ULONG (System.UInt64), char (System.Char).

Float type: float (system.single), double (system.double).

High-precision decimal type for financial calculations: decimal (System.Decimal).

BOOL Type: bool (alias of System.Boolean).

User-defined struct (derived from System.ValueType).

Enum: Enum (derived from System.Enum).

Nullable type.

Each value type has an implicit default constructor to initialize the default value for that type.

For example:

int i = 0;

Equivalent to:

int i = new int ();

When you use the new operator, a specific type of default constructor is called and the variable is given a default value. In the example above, the default constructor assigns the value 0 to I.

All value types are sealed (seal), so new value types cannot be derived.

It is worth noting that System.ValueType is directly derived from System.Object. That is, System.ValueType itself is a class type, not a value type. The key is that ValueType overrides the Equals () method to compare the value type against the value of the instance, rather than the reference address. You can use the Type.isvaluetype property to determine whether a type is a value type:

Testtype Testtype = new Testtype ();

if (Testtypetype.gettype (). Isvaluetype)

{

Console.WriteLine ("{0} is value type.", testtype.tostring ());

}

(2) Reference type

C # has some of the following reference types:

Array (derived from System.Array)

The user needs to define the following types.

Classes: Class (derived from System.Object);

Interface: Interface (interface is not a "thing", so there is no question of where to derive it.) The interface simply represents a contract convention [contract]).

Delegate: Delegate (derived from System.Delegate).

Object (alias of System.Object);

String: String (alias of System.String).

Can be seen:

A reference type is the same as a value type, a struct can also implement an interface, a reference type can derive a new type, a value type cannot, a reference type can contain a null value, a value type cannot, and an assignment of a reference type variable copies only the reference of the object, not the object itself. When you assign a value type variable to another value-type variable, the contained value is copied.

C # and OOP knowledge summary

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.