Knowledge of C # classes

Source: Internet
Author: User
Tags mathematical functions value of pi

  In English, the class is the root word of the classification (classification). The process of designing a class is to classify the information and put the relevant information into meaningful entities.

Purpose of Encapsulation:

Encapsulation is the important principle of defining classes. The central idea is that a program that uses classes should not be concerned about how the classes work inside. Program values need to create an instance of the class and Invoke methods in the class. Class in order to implement its existing methods, it may be necessary to maintain a variety of internal state information, but also in the class of various activities, in the use of classes in front of the program, these additional state information and actions are hidden, so sometimes called information hiding.

1. Merging methods and data into classes, that is, to support classification.

2, control access to methods and data, but also in order to control the role of the class.

Define and use classes:  

  C # uses the class keyword to define a new class. The data and methods of the class are placed in the body of the class.

First create a new class. The code in the class is as follows

  

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;//using System.Text.RegularExpressions;namespacetesetdata{classClass1 {//apply a variable to store the circle radius        intradius=2;  Public DoubleArea () {//The math class contains methods for performing mathematical calculations and also defines some mathematically solid math.pi containing fields 3.14159265358979 pi//            //Summary://provides constants and static methods for trigonometric, logarithmic, and other general-purpose mathematical functions. //to explore this type of. NET Framework source code, see Reference source.             returnMath.PI * RADIUS *radius; }    }}

Main function Section:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacetesetdata{classProgram {Static voidMain (string[] args) {Class1 Test=NewClass1 ();//instantiate Class1 this classConsole.WriteLine (Test.area ());        Console.ReadLine (); }    }}

Tip: The variables declared in the method are not automatically initialized, but the fields in the class are automatically initialized. The fields of the class are automatically initialized to 1,false,null, and the specific initialization is more type-determined, in principle, the manual initialization variable.

  C # naming and accessibility

The public identifier begins with a capital letter, because he is public, which is called the Pascalcase naming method,

Non-public identifiers, including local variables, start with a lowercase letter, because he is private, that is, the CamelCase naming method

Note: Do not declare two public members with a different case name, and the port is not available in case-insensitive languages.

The above example creates a Class1 class. In this class, we apply an int data and assign a value of 2, then call Math.PI, the approximate value of pi, double type data. Calculation of the time Circle area.

One of the problems with this example is that you cannot access the RADIUS variable, so you can no longer modify the parameters of the variable outside of the class. Workaround using the constructor

    

Knowledge of C # classes

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.