C # Basics (3)

Source: Internet
Author: User

One, constant:

Grammar:

Const type constant NAME = variable name

Assign values when defined, not allowed in other places

Second, enumeration:

Let's define a type and when we define this type, we want to specify all values of this type

Grammar:

Enum (self-starting type name) {value 1, value 2, 、、、、 value n}

For example: Define an enumeration type called gender, which has a value of only two, male and female

Enum Gender

{

Male, female

}

The definition of an enumeration is generally defined at the same level as the class, so that all classes under the same namespace can use this enumeration (or in a method/Class)

The role of the enumeration:

(1). Restrict users from arbitrarily assigning values, only in the values enumerated when defining enumerations

(2). Does not need to rote each value is what, only need to select the corresponding value

Note: When defining enumerations, values cannot be of type int

Variables of enum types can be cast to an int type

The value of the enumeration has a default number at the time of definition, numbering starting from 0

If you convert a string to an enumeration type:

Formula: (Self-defined enumeration type) (Enum.parse (typeof (self-defined enumeration type), "string to convert")

Typical enumeration type: parameters of the Message.show

Third, Structure:

A struct is also a type, and can contain variables and methods

Grammar:

Access modifier struct struct name

{

defining struct Members

}

Once the good one structure is defined, it is possible to declare the corresponding variable directly, and after declaring the variable, access the member by the variable name. Member Name

Four, array:

Can help us declare multiple variables of the same type at once, which are stored continuously in memory

Grammar:

Array type [] array name =new array type [array length]

Example: int[] score=new int[5]

How to access an array: Accessing an array by subscript (index)

Once the int array is declared, each element inside is initialized to 0.

Through the array name. Length to get the lengths of the array

Console. Clear (): Clear screen

Five, bubble sort:

Let the elements in the array 22 compare (I and i+1), after i-1 22 comparison, the elements in the array can be sorted according to the law we expect; to sort from large to small, we make 22 comparisons with < to sort from small to large, we do 22 comparisons with >

Formula:

for (int i=0;i<numbers. length-1;i++)//number array name

{

for (int j=0;j<number.length-1;j++)

{

if (condition)

{

Exchange

}

}

}

Vi. methods:

Function: Used to reuse code, when we write the same code repeatedly in a program, it is generally possible that we can define the code that needs to be duplicated in a method, only need to call it when using it.

Syntax for defining methods:

[Access modifier] [Static] Return value type method name ([parameter])

{

Method body

}

Attention:

(1). Generally, methods are generally defined in the class

(2). If the method does not return a value, the return value type write void

(3). If the method has no arguments, the parentheses () cannot save

Naming rules:

The method name starts with uppercase, parameter names are capitalized, parameter names, variable names make sense

Method is called:

The class name is used for static methods (modified by Static). Method name; If you call a method in this class in the same class, you can write only the method name ();

Once a variable is defined in the outside of the method and inside the class, it is called a field of the class, and the variable can be accessed by all methods of this class, but note that static methods cannot access static fields

Scope of variables:

A variable defined in a method is called a local variable, and its scope is defined from the beginning of the definition to the end of the curly brace where it is located.

Eight, return value:

Grammar:

return value;

Attention:

A method can have only one return value

Once a method has a return value, in this method, you must return a value through the return statement, and the value is the same as the return value type

Nine, Heavy:

In the same class, the method name is the same, and the number of parameters of the method is different or the type at the corresponding position is different, the method can be overloaded

For example:

public double static Shu (int a,double b) and public double static Shu (double A,int b)

Or

public double static Shu (int a,int b,int c) and public double static Shu (int a,int b)

Attention:

The overloads of the method are independent of the return value

X. Out parameters and ref parameters for methods

To return values by parameter:

static void Main (string[] args)//caller

{

int number;

int Result=test (out number);

Console.WriteLine ("Number={0},result={1}", Number,result);

}

static int Test (out int a)//callee

{

a=10;

a=a+1;

return 200;

}

As above code:

Implementation steps:

(1). Before the parameter type of the method is added out, then the argument must be added out before the number, indicating that the parameter is not passed in, but is used to transfer the value of the

(2). If the parameter is passed in as out, the initial value may not be assigned before passing in

(3). In a method, a value must be assigned to an out-decorated parameter, and must be assigned before use

Out is internally assigned to an external variable, and out is generally used where the function needs to have multiple return values

Out is used for outgoing values, which must be assigned to an out-decorated parameter in the method

Ref can be interpreted as bidirectional, can be passed in or out

In the process of passing parameters, if the parameter has an out or ref modifier, then change the value of the parameter variable in the method, the value of the variable in the caller's method will also change accordingly

C # Basics (3)

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.