C # Study Notes (September April 4)

Source: Internet
Author: User

In C #, you can use methods to organize applications into more structured individuals. A Method is a member of a class and can be used to execute a job or calculate some numeric values.
One method is a collection of C # programming languages with a name for easy identification. The Main method is the entry point of every C # application.

To end the execution of a method immediately and return the execution process to the Code section of the program that calls the method, you can use the return statement.

The method parameters include By Value, By Reference, and Output ).

Finally, C # allows two or more methods with the same name but different function signatures in the same class, called overwrite methods, to increase the flexibility of programming.

During programming, two data structures are often used to group data: structure (struct) and Array (Array). The differences between the two are as follows: structure is used to organize different types of data together; arrays are used to organize data of the same type together.

When using arrays in C #, pay attention to two things:

Array names cannot be placed in front of brackets (int [] MyArray, one-dimensional Rank1; int [,] MyArray two-dimensional Rank2)
When declaring an array variable, the array size cannot be specified.
Use Length to check the upper and lower limits of the array.

For (int I = 0; I Array initializer initializes when declaring an Array
Int [] MyArray = new int [4] {1, 2, 3, 4}; (this can be simplified to int [] MyArray = {1, 2, 3, 4}
Initialize multi-dimensional arrays:
Int [,] MyArray = new int [2, 3] {
{34,54, 33 },
{44,33, 44}
};

Arrays actually inherit the System. Array class. Therefore, the attributes and methods of the Array can be used:

Rank attribute: obtains the dimension of the array.
Length attribute: specifies the number of array elements.
Sort method: used to Sort elements in an array.
The Clear method is used to set the element in a range in the array to 0 or null.
Clone method: copy the array content to a new array object.
The Getlength method is used to return the length of a certain number.
IndexOf method: returns the value that matches the specified parameter value in the array value and appears for the first time.

Declaring an array variable does not represent the entity of the generated array. This is because the array belongs to the reference type, not the real value type. The new reserved word must be used to generate an array entity and specify the size of the dimension (Rank.

Object-oriented

Using System; public class Employee {public void SetEmpID (string EmpID ){File: // setSet employee code mstrEmpID = EmpID;} public string GetEmpID (){File: // getEmployee code return mstrEmpID;} public void SetEmpName (string EmpName ){File: // mstrEmpName = EmpName;} Public void GetEmpName (){File: // returnMstrEmpName;} public static SetCompanyName (string CompanyName ){File: // mstrCompanyName = CompanyName;} Public static GetCompanyName (){File: // returnMstrCompanyName;} private static string mstrCompanyName = "; // private string mstrEmpID ="; // private string mstrEmpName = "";File ://}Public class HumanResource {public static int Main () {Employee. setCompanyName ("AI361 STUDIO"); Employee e1 = new Employee (); Employee e2 = new Employee (); e1.SetEmpID ("001"); e1.SetEmpName ("lsmodel "); e2.SetEmpID ("002"); e2.SetEmpName ("mermaid"); Console. writeLine ("Employee 1: company name = {0}, Employee code = {1}, Employee name = {2}", Employee. getCompanyName (), e1.GetEmpID (), e1.GetEmpName (); Console. writeLine ("Employee 2: company name = {0}, Employee code = {1}, Employee name = {2}", Employee. getCompanyName (), e2.GetEmpID (), e2.GetEmpName (); return 0 ;}}


The main purpose of Data Encapsulation:

U data and methods are encapsulated into a single element for data access through methods.
U can control the data access mode.
U achieves data hiding.

Static Member

A static class is used to describe the information of all objects of a class. In other words, you can use static data without having to create an object. The memory of static data is allocated to a globally shared block, and all classes of entities can be accessed.

Static data is set to private. Setting static data to public is a good way to encapsulate static data. Static methods are treated as global functions in Compiler compilation. The lifecycle of a static member is limited to the lifecycle of the application. This reserved word cannot be used for static methods. This is a pointer to the object itself. In other words, static methods can only access static data and other static methods.

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.