C # Quick Start Guide

C#

Integrated Development Environment

Visual Studio

structure

using System;

It includes the System namespace

Hello {class     / Comment * * / 
static void the Main (String \ [\] args) { // file a main function } }


Structure

variable struct Books {...}

Structures do not support inheritance.
Structure can not declare a default constructor

enumerate

enum {Day Sun, Mon, Tue, Wed, you, Fri, Sat};

Day.Sun==0

Day.Mon = = 1

interface

public interface PaintCost {int getCost(int area);}

Derived class

class Rectangle : hello, PaintCost{}

Full Analysis

namespace first{

Namespace declaration

    public class Test{

It may be a plurality of class
constructors, destructors, static members ...

variable...

		decimal a=10;

Exact decimal value 128, effective number of bits 28-29

		sbyte b = 2;

8-bit signed integer type

		uint c;

32-bit unsigned integer type

		ulong d;

64-bit unsigned integer type

		ushort e;

16-bit unsigned integer type

		dynamic f=a,g=b;

Store any type of data type values ​​in the dynamic variable

String

		"hhh\"hhh\nhhh"	==
		@"hhh"hhh\nhhh"	==
		@"hhh"hhh
			hhh"
		"hhh" + "hhh"

String a bunch of methods: split, copy ...

Nullable types, i.e., can be assigned to null

		int? a,b=null;	//a==null
		int a;		//a==0

Merge Operators

		int b = a ?? 5; // a null to b = 5, b = a or

Multidimensional Arrays

		int [,] = A new new int [3,4-] { 
			{0,. 1, 2,. 3}, / * initialize the index number of rows 0 * / 
			{4, 5, 6, 7}, / * initialize the index 1 row * / 
			{8, 9, 10, 11} / * initialize the index number of the row 2 * / 
			};

Staggered array

		int[][] scores = new int[5][];
		for (int i = 0; i < scores.Length; i++) 
			scores[i] = new int[4];

Array class

		scores.Rank; // Get rank (dimensions) of the array 
		scores.Length; 
		Array.Copy (the Array, the Array, Int32); 
		/ * start copying a range of elements from the first element of the array 
			to another array of position of an element * /

Other Operators

		& a // address 
		* a // pointer 
		obj is Car // check if obj is an object of the Car class. 
		obj as int // cast, even if the conversion fails it will not throw an exception.

constant

		const

Other constants such as octal

		0213

Boxing and unboxing

		obj Object; 
		obj = 100; // this is boxed 
		int a = obj as int; // this is unboxing

cycle

		foreach (variable in array) {}

method...

		public int plus(int a){return a+1;}

Overload

		public int plus(char a){return a+2;}

Operator overloading

		public static Box operator+ (Box b, Box c){...}

Overloadable

  • + , - !,, ~ , + + , - - (on-type operand)

  • + , - , * , \ /,%, = = , ! = , < , > , < = , > = (Op-type operand, on-type2 operand2)

Not directly overload

  • &&, ||

Not overload

  • +=, -=, *=, /=, %=,=, ., ?:, ->, new, is, sizeof, typeof

Passed by value

		(int a)	

Quote

		(ref int a)

Pressing the transfer, that is, the last variable value assigned back

		(out int a)

Pass an array to a function

		(int[] a)

Parameter array

		int plus(params int[] arr)

Delivery time:

		plus(512, 720, 250, 567, 889)

access permission

public: all objects can access; Internal: a set of objects of the same program can access; protected Internal: restricting access to the current type self-contained assembly or derived class.

	}
}

Namespace declaration

namespace second{
	public class Test{
	}

Nested namespace

	namespace second{
	}
}

Using namespace

class TestClass{
	void hhh(){
		first.Test;
		second.Test;
	}
}

using instructions

using second; // using directive: the introduction of namespaces 
using static System.Math; // specify the type of access to its static> members without specifying the type name 
using Project = PC.MyCompany; // aliases 
using (Font font3 = new font ( "Arial", 10.0f) , // the code to bind instance 
         font4 = new font ( "Arial" , 10.0f))

Preprocessor directives

#define PIQ // define a series of character symbols become 
#if (PIW) {} // test does not perform symbol is true 
#elif (PIQ) {executes} 
#else {} 
#endif // specifies a conditional instruction the end 
#undef PIQ // undefines symbol 

#line number filename modify the number of rows // compiler and (optionally) output error> error and warning file name 
#line default // restore the default line number 
#error you're wrong up! // generate an error code from the specified location 
#warning you are wrong! // the specified position code is generated from an alert 
warning #pragma warning disable 169 // cancellation number 169 (i.e. field> Warning unused) 
#pragma warning Restore number 169 169 // restore warning 

#region // use when the outline characteristic of Visual Studio Code Editor, expand or collapse a specified code block 
#endregion // end of the identification block #region

Example usage

#define CONNECT (a, b) a ## b // ## adhesions two identifiers, used only macro definitions (#define) 
int the CONNECT (A,. 1); // int A1

The traditional way:

typedef struct _tag_Student Student;
struct _tag_Student{
	char* name;
	int id;
};

By way of macro definition

#define STRUCT(type)    typedef struct _tag_##type type;\
		       struct _tag_##type
STRUCT(Student){
	char* name;
	int id;
}; 

Exception Handling

try	{throw a;}		//a为throwable
catch(a)	{}
finally	{}

In the catch block using the throw statement to throw the current object

_throw {class 
void _throw () { 
the try { 
	MethodThatThrowsException (); 
} 
catch (Exception EX) { 
	// do not exist and the same time the catch blocks 
	the throw; 
} 
catch (Exception EX) { 
	// Stack catch to the trace that you exception has been handled 
	// only process but also throw new exception 
	// stack trace at this time put throw ex; as a root cause of the error 
	throw ex; 
} 
the catch (exception EX) {	 
	// stack trace will automatically considered internal abnormality is the cause of the current abnormal 
	// it will also inner exception stack trace recursion displayed 
	the throw new new Exception (, EX "oops!"); 
} 
}}

System.ApplicationException 类:

Support for exceptions generated by the application. So the exception should be derived from the class programmer-defined.

Input and output

System a using; 
class _IO { 
void _IO () { 
	Console.Write ( "C # is lost"); 
	Console.WriteLine ( "Information displayed"); 
	( "the first student of Console.WriteLine name {0}, {achievements 1} ", NAME1, score1); 
		//" format string "and a list of variables, when there are multiple variables by using this method can 
		// {0}, {1} is called placeholder sequentially arranged behind the representative variable table 
	console.readkey (); // wait for a user presses any key, read into a character 
	console.readline (); // string, when the user presses the enter key once read one line 
	convert.toint32 / todouble ( "32"); // convert for conversion between different types of data 
	int.Parse ( "32"); // this can also transform 
}}

Regular expression: match dedicated, it can be used to enter readline () finishing

Regex class for representing a regular expression

Read for yourself ~

Input and output file

Characteristics, similar to Java's / * * * / ** generate Instructions Comments  

[attribute(positional_parameters, name_parameter = value, ...)]

class || method || variable ......
read for yourself ~

reflection:

Allowed (attribute) View at runtime characteristics.
Review types allows the collection, as well as examples of these types.
Delay to allow binding methods and properties (property).
Allows you to create a new type at run time, and then using these types perform some tasks.

Unsafe code

Because the variable C # declared by the garbage collector memory management memory; therefore a variable (e.g., a large array) is likely to be moved to another location in the memory during operation. If the memory address of a variable is changed, then the pointer would be no meaning.

The solution is to use keywords to fixed variable fixed position does not move.

int[]  list = {10, 100, 200};
fixed(int *ptr = list)

Or the method indicated in unsafe

public unsafe static void Main(){
	int var = 20;
	int* p = &var;
}

Or procedures in marked unsafe

unsafe{	int var = 20;
	int* p = &var;
}

Note that you need to modify the command-line compiler or compiler settings when compiling

Guess you like

Origin www.cnblogs.com/rsmx/p/11829575.html