C # Summary

Source: Internet
Author: User

The first chapter:

1.c# learning, divided into three parts: Net Farmeword, C # Basic syntax, studion tools

2.Main () method

As with Java, it's a portal to the program.

The Main () method has four types, parameters are optional, and the return value can be void and int

static void Mian (string[] args) {}

static int mian (string[] args) {}

static void Mian () {}

static int Mian () {}

Note: Main () Capitalize first letter

3. Program Structure

NET Java

Import a namespace (using) Import package (import)

Claim namespace (namespace) Declaration Pack (package)

class declaration (class, omitted public) class declaration (public Class)

Main method (initial capitalization) main (all lowercase)

4.c# variable

Variable declaration: Same as Java declaration

Data type variable name;

Naming rules: Letters, Numbers, "_"

First letter: Letters, "_", "$" in Java cannot be used in C #

Data type:

C # java

String string

BOOL Boolean

5.c# Constants

C # Definition: const data type constant Value name = value;

Initialization must be performed at the time of declaration.

Constant naming rules:

A. Meaningful

B. All Caps

C. Length should not be too long (less than 25)

When to use constants

1. Non-random changes

2. Multiple references

3. The value has a special meaning

6.Console class

Method: Output Console.Write (); Output No line break

Console.WriteLine (); Output line break

READ: Console.read (); Read Next character

Console.ReadLine (); reads the next line of characters

Note: the Read () return value type is int

The ReadLine () method returns a value of type string

Cast in C #: Int. Parse (parameter of type string);

7. Classes and objects

Class and object relationships: Classes are abstractions of objects, objects are potential instances of classes

Definition: Access modifier class class name {

}

Method definition: The behavior of a class

Method function: Easy to modify, reusability, encapsulation

Syntax of the method:

Access modifier return value type method name (parameter list)

{

Method body

}

Note: Method name: The first letter of each word is capitalized with Pascal's name

Notes in the 8.c#

Java C #

Line Comment:////

Block Comment:/*/*

*/        */

Documentation notes:/**//

*/         ///

///

9. Preprocessing

#region

#endregion

Collapse code to make the code structure clearer

Pre-processing commands for chunking

Chapter II

1. Select structure

C # is the same as Java:

Four kinds:

Basic: If-else:

if (condition) if (condition)

{                              {

After the condition has been established, the action performed}

} else

{

}

Multiple if: nested IF

if (condition 1) if (condition 1) {

{if (condition 2) {

Action 1//Operation 1

}else if (condition 2) {}else{

Action 2}

else}else{

{                           }

Action 3

}

Switch structure

Java differs from C #

Java C #

Different points: type: Int/char int/char/string

The case after the break keyword can be omitted must have

Default after break can omit must have

Same point: The value after the case must be a constant value

Value cannot be the same after case

2. One-dimensional arrays

C # Definition: Data type [] data name;

Specify array Length: data type [] array name =new data type

data type [] array name ={element}

data type [] array name =new data type [number of length]{elements}

Note: Specify the number of brackets in parentheses, and specify the same length as a value,

Initialize: Array name =new data type {Number of elements}

3. Cyclic structure

Three loop structures in Java, with the same syntax in C #

While: First judge the condition, the establishment is executed, does not establish the end loop

Do-while: First execute once, then judge the condition, set up the execution, do not set up the end cycle

For: Initialize first, then judge, set up execution, do not set the end loop, generally used in a fixed number of cycles

New loops in C #:

foreach (element type identifier (variable name) in array name)

{

code block

}

Note: The foreach identifier, which is a read-only variable that cannot change its value execution, takes each element of the array in turn, and is often used to traverse an array or collection

4.break and Continue

Break applies To: loop structure, with switch selection structure

Function: To end this loop, or to end the structure of a selection

Continue is only used in loop structures

Function: End this cycle and proceed to the next cycle

Chapter III

1. Access modifiers

Public is not subject to any of the most restrictive levels

Private only use other classes within the class cannot use the lowest level

2.this keywords

This provides access to all member variables and methods of the current class

3. Properties

Note: The attribute must be the same as the field type

Property has two accessors, get set

Get accessor: Returns the field name

Set accessors: Setting field values

Read-only property: Write-Only Get method

Write-only property: Write-only set method

Properties: Encapsulates the field to improve the security of the data.

4. Assigning values to properties using object initialization

Class Name Object name =new class name () {attribute 1= value 1, Property 2= value 2}

5. Value passing and reference passing

Value passing: equivalent to copy

Reference delivery: equivalent to shared

So, value passing does not change the value of the argument, and the reference pass changes the value of the argument

How value passing becomes a reference pass, using the REF keyword

Both the argument and the formal parameter must be ref

such as: Invocation: Method name (ref variable name)

Definition: Access modifier return value type method name (ref type variable name)

{

}

Classification of data types: Basic types and reference types

Basic type Default: Value passing

Reference type Default: Reference pass (except string) without ref

Basic types are: Int,double,char,bool

Reference types: String, type, array, etc.

Fourth Chapter

1,string class method

IndexOf (character or string): Returns the location of the lookup character (string);

Zero-based, if not found, returns 1

Substring (start position, length) to intercept the string; Example ABCDEFG. Substring (5,2) results returned; FG

ToLower (): Converts a string to lowercase;

Toupper (); Converts a string to uppercase;

The difference between 2.== and Equals ()

= =: Compare addresses

Equals (): Comparison value

Except for string types in C #, use = = with equals effect

3. "" with string. The similarities and differences of empty

Same: Both refer to null characters

Difference: "", allocate the space of the deposit with blank length

String. Empty: Cannot allocate storage space

4. Three ways to judge if a string is empty

String. length==0;

String = = "";

string, = = String. Empty;

Efficiency: String. Length==0> string ==string. empty> string = ""

5.split (). Join ()

Split (): Variable name. split (char type delimiter)

Join (): String. Join (connector. Connection object)

6.Format ()

String mgstring=string. Format (formatted string "parameter list")

Include fixed text and format items in a formatted string

Format Item Syntax: {Index [, align] [: Format character]}

Alignment: Right-aligned, negative left-justified

formatting characters,

Character Declaration Example output result

C Currency format String.Format ("{0:C3}", 2000) ¥2000.000

D decimal Format String.Format ("{0:d3}", 2000) 2000

F number of digits after the decimal point fixed String.Format ("{0:f3}", 2000) 2000.000

N number String.Format separated by commas (,) ("{0:n}", 250000) 250,000

P percent Count Method String.Format ("{0:p3}", 0.29768) 29.768

X hex Format String.Format ("{0:x000}", "C")

7. Type Conversion

Two main classes: implicit conversion (auto-conversion) Condition: type compatible; target type is greater than source type

Explicit conversions (casts)

1. Data type: Generally used for numeric conversions will lose precision

2. Data type. Parse (String object): Converting a string to another type

3.convert.toxxx (object): Convert any type to another type:

Method description

Convert.ToInt32 () to Integer (int type)

Convert.tosingle () converted to single-precision floating-point (float type)

Convert.todouble () converted to double-precision floating-point type (double)

Convert.ToString () converted to string type (string)

C # Summary

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.