[C #] Chapter 1 learning points (ii) custom classes and structures,

Source: Internet
Author: User

[C #] Chapter 1 learning points (ii) custom classes and structures,

Category: C #, VS2015

Created on:

Teaching materials used: (12th Five-Year Plan teaching materials) C # program design and application tutorial (version 3rd) I. Key Points

The classes provided by others are used to simplify your workload, but the actual processing is very different. You have to define the classes by yourself to write code to do the actual work. Therefore, first understanding how to customize the class and its related concepts and key points is the first level that cannot be bypassed by writing programs. Ii. Basic concepts of classes and members

1. Basic Format

[Access Modifier] [Static] classClass Name[:Base Class[, Interface Sequence]

{

[Class Member]

}

Key points:

(1) A base class can have at most one. If the base class is not specified, it is inherited from the System class by default.

(2) class members include fields, attributes, constructors, methods, events, operators, indexers, and destructor.

(3) In most cases, you do not need to define the destructor. You must define the Destructor only when you define the resources to be recycled rather than automatically cleaning them by garbage collection.

Example:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApplication1 {class Person {private static int n = 1; public string Name {get; set ;}= string. empty; public int Age {get; set ;}= 0; public Person () {Console. writeLine ($ "Number of {n ++} calls");} class Program {static void Main (string [] args) {Person p1 = new Person () {Name = "zhangsan", Age = 20}; Console. writeLine ($ "Name: {p1.Name}, Age: {p1.Age}"); Person p2 = new Person () {Name = "Li Si", Age = 22}; Console. writeLine ($ "Name: {p2.Name}, age: {p2.Age}"); Console. readLine ();}}}

2. Access Modifier

(1) Basic access modifier

Public: internal and external code of the class can be accessed.

Private: the class can be accessed internally, and the class cannot be accessed externally. If the class member access modifier is omitted, the default value is private.

Internal: code in the same assembly can be accessed, and other code outside the Assembly cannot be accessed. If the class access modifier is omitted, the default value is internal.

Protected: the class can be accessed either inside or from the subclass inherited from the class.

Protected internal: Class inherited from this class or inherited from another set of programs can be accessed.

(2) Special access modifier

When you describe the same class with multiple different files, you can also add the partial modifier after the basic access modifier. For example, this can be used when multiple users write the same class at the same time.

When the compiler compiles the source program, it automatically merges it into the same class.

3. Constructor

Key points:

(1) constructor is a function automatically called when an object is created. Generally, some initialization work is done in the constructor, or some specific operations that only need to be performed once.

(2) The constructor does not return a type and its name is the same as the name of its class.

(3) constructors can be overloaded.

(4) when calling the methods provided by the base Class in the constructor of the extended class, use "base. method name.

4. Static classes and static methods

Key points:

(1) adding static before the method name indicates that this is a static method.

(2) adding static before the class indicates that the defined class is a static class. In this case, all the members of this class must be static (all must contain static modifiers ).

(3) in C #, the static method is called by class name. method name, And the instance method is called by instance name. method name.

(4) static methods can be overloaded but cannot be overwritten.

Note:

(1) The declared static member has only one copy in the memory.

(2) The first call is loaded to the memory, and all Members are removed from the memory when no call is made. These tasks are automatically completed by the garbage collector.

5. Attributes

Key points:

(1) property is an extension of a field and is used to read and write fields.

(2) The difference between a property and a field is that the attribute does not represent the storage location, but the statement to be executed when the read/write field value is specified through the get accessor and set accessors. Depending on the usage, you can only provide get accessors or set accessors, or both.

(3) basic usage example:

Public string Name {get; set ;}= string. Empty;

Public int Age {get; set ;}= 0;

Public int Grade {get; private set ;}

6. Method

Key points:

(1) when defining a method, the following keywords can be used before the parameter name:

Ref, out, params

(2) If you do not add a keyword before the parameter name when defining a method, you must note the differences between the call of the value type and the reference type by default.

(3) method Overloading

Multiple methods with the same method name but different parameter types or number of parameters can appear in the same class at the same time. In the project development process, many methods need to be implemented using method overloading.

7. Events

Events are implemented by delegation. Because the project uses a lot of delegation and events to complete various functions, we will introduce it in a separate chapter later.

8. Class encapsulation, inheritance, and Polymorphism

It is an advanced usage and has a lot to do with programming experience. It is a difficult topic for beginners, but it is also the best place in the project to reflect the advantages and differences of core technologies.

(1) Encapsulation

Abstract class: declare with abstract keywords. Abstract is to extract public items and put them into a separate class. This requires that the extended class must implement some methods that only declare unimplemented in the abstract class, you can also directly implement some common methods in the abstract class.

Seal class: declare with the sealed keyword. This keyword is used in the definition of a class to indicate that the class cannot be inherited. Using this keyword in the definition of a method indicates that the method cannot be overwritten.

(2) Inheritance

The purpose of inheritance is to avoid repeated Writing of the same Code. If you describe it in a less rigorous way, you can understand it as Stratified Management of the things to be handled.

Pay attention to the processing of constructor in inheritance.

(3) polymorphism

Note the usage of the following keywords:

Virtual and override: virtual is used in the method definition of the base class to indicate that the method can be overwritten by the extended class. override is used in the extended class to indicate that the method is rewritten by the same name in the base class.

New: the virtual declaration is not used in the method definition of the base class (that is, rewrite is not allowed), but you still need to rewrite it. In this case, you can use this keyword. Iii. Custom Structure

Key points:

(1) Use the struct keyword to customize the structure. From the definition form, the difference between it and the class is only one word (the structure is declared with the struct keyword, and the class is declared with the class keyword), and the other is the same.

(2) The structure cannot be inherited.

(3) The difference between the structure and the class is the performance during execution: The variable values of the structure are saved in the stack (FAST ), the variable values of the class are saved in the heap (a little slower ).

(4) Any custom structure can be implemented using a custom class. If you can't figure out whether to use a structure or a class, you don't need to consider performance for the moment. You just need to use the class to implement it. With the increase of development experience, you will naturally understand which situations should be implemented using the structure.

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.