Basic summary of C # programming

Source: Internet
Author: User
Tags arrays case statement constant constructor hash modifier resource sort

. NETFramework Introduction

. NETFramework runs on top of the operating system, providing good cross-language features.

. NETFramework contains two content: Common language Runtime (CLR) and class library set (FCL)

MSIL Microsoft intermediate Language. When compiling code written in a. NET supported language, the output code is MSIL

The CLR also contains: a common Language Specification (CLS: A set of rules that guarantees language interoperability) and a common type system (CTS: Contains data types and features compatible with. NET support)

Variables and constants in C #

Basic data types in C #:

Value types and reference types

Value type: Simple Type, struct type, and enum type.

Simple type: integer type, floating-point type, decimal type (decimal), Boolean type, etc.

SByte is a signed, rather Java-byte, range -128~127

The byte in C # is unsigned, the range 0~255

Variable naming method:

Pascal's nomenclature and Camel Naming act

Pascal nomenclature: If you have more than one word, capitalize the first letter of each word

Camel nomenclature: If there are more than one word, the first word is all lowercase, and the following words are capitalized

Constants in C #: Two kinds of const and readonly

Const declared constants: Called Static constants, must be initialized when declared, and can only be initialized with constant values.

ReadOnly declared constants: Called dynamic constants that can be uninitialized at declaration time, only initialized in constructors, but must be initialized in each constructor, and can be initialized with variable values.

Class Test

{

const float PI = 3.1416f; Constant name: All caps

readonly float G;

Public Test ()

{

G = 9.80F;

}

The public Test (float g)//is initialized in each constructor, and can be initialized with a variable value.

{

g = g;

}

}

Boxing: Value types are converted to have reference types

Unboxing: Conversion of reference types to value types

Value type: Existing stack

Reference type: The address of the object (that is, the reference) in the heap, and the object itself is stored in the stack

Unboxing allows value types and reference types to handle each other

C # syntax

Boxing and unboxing in C #

Switch () brackets can be int, char and string, the case statement in the switch statement does not write a colon, you can write a break, other circumstances must write a break, or error

Arrays: Five Ways of declaring

int []array;

Array = new INT[2];

The second way of declaring

int []array1 = new int[2];

The third way of declaring

int []array2 = {1,2,3};

Fourth way of declaring

int []array3 = new int[]{1,2,3};

Fifth way of declaring

int []array4 = new int[3]{1,2,3};

The size of an array can also be a variable

int count = 3;

int []arr = new Int[count];

Enumerations: Accessing data with meaningful characters

public enum Contry:long//Specify enum type, must be integral type, not write int

{

Pacific,//First unpaid value, default to zero

china=1860,

Japan,

us=1901,

Canada

}

Object-oriented in C #

destructor: The function name is the same as the constructor name, the ~ function name (), which does not accept parameters, is automatically invoked by the garbage collector (GC. Collect () calls the garbage collector)

Virtual keyword: In C #, subclasses must override the parent class's method by identifying the method of the parent class as virtual (dummy) While overriding the method with the override modifier

New keyword: New keyword: one defined in subclasses is the same as the parent class method signature, but a new method. is not a method that overrides the parent class.

Base keyword: method to invoke the parent class with the base keyword

Access modifiers:

Public-owned,

Internal in a project,

Protected classes that have a parent-child relationship,

Private only members of the owning class

Note: If a class inherits both a class and implements an interface, the class name is written in front of the interface name.

Properties, indexers, delegates, events

Properties: The access modifier is generally public, and the first letter is capitalized. The property has a get and set accessor, and a get must have a value keyword inside the return,set, representing externally accepted values.

Indexers: The role of indexers: objects that handle classes like arrays.

public class Student

{

private string []obj=new string[10];

The This keyword here represents the object for each class, and the integer in [] refers to access through the subscript

public string This[int Index]//This is a member of each class that can be accessed by index number

{

Get

{

return Obj[index];

}

Set

{

if (value!=null)

Obj[index]=value;

}

}

static void Main (string []args)

{

Student stucollection=new Student ();

Stucollection[0]= "Conan";

stucollection[1]= "Small Yogoro";

Stucollection[5]= "Strange Thief Kidd";

}

}

Delegate: the equivalent of a function pointer, which enables a program to run to specify a method to run.

(1) Define delegate: public delegate int call ()

(2) Instantiation of the delegate: Objcall=new Call (method name)

(3) Call the delegate: Objcall ();

Event: An event is actually a special delegate, which can point to only one method at a time, and events can point to multiple methods

(1) Define a delegate public delegate void Delegateme ();

(2) Define an event delegate Eventme;

(3) Subscribe to Event Eventme+=new Delegateme (method name 1 ());

Eventme+=new Delegateme (method Name 2 ());

(4) Triggering event if (condition) then eventme ();

Multithreading

To create a thread instance:

Thread Obj=new Thread (new ThreadStart (method name))

Start: Start ();

Hibernate: Sleep ();

Termination: Abort ();

Hang: Suspend ();

Recovery: Resume ();

Current thread: Thread.CurrentThread

ThreadPriority enumeration value is used to specify the priority of the dispatch thread (Total level 5)

Lock keyword

C # provides synchronization by lock keyword

Thread synchronization: Ensures that when different threads access a shared resource, only one thread accesses the resource at a time.

Lock (This)

{

for (int i=0;i<10;i++)

{

Statement

}

}

Array Collection Object

Array:array and arrays are very similar, and can be converted and copied, many methods are universal, can be used array static method of the array to achieve inversion, sorting, which is not possible

Using System. Array;

Array ar=array.createinstance (typeof (int), 5); Create an instance of an array

Ar. SetValue (12,0); assigning values

Array.reverse (AR); Reverse

Array.Sort (AR); Sort

ArrayList: One of the most common collections. The benefit of the collection is that the capacity can grow automatically when you don't know the size of the data, and the array does not.

Add () Adding elements

Remove (position) element removal

ArrayList al=new ArrayList ();

If you want to traverse the collection element,

Method One:

Copy the number in the collection into an array

Object []temp=al. ToArray ();

foreach (Object T in temp)

{

Console.WriteLine (t);

}

Method two, using iterators

IEnumerator Ie=al. GetEnumerator ();

while (ie. MoveNext ())

{

Console.WriteLine (ie. Current);

}

HashTable: Saving values as a key-value pair

Hashtable hash=new Hashtable (4);

Hash. ADD ("China", 1860);

Hash. ADD ("Germany", 1940);

Console.WriteLine (hash["China"). ToString ()), obtaining value through key

SortedList: A mixture of Hashtable and Array

can store key value pairs, similar to Hashtable

Can be traversed directly through the index through its own method, similar to array

Objsortlist. Getkey (i) method to get keys

Objsortlist. Getbyindex (i) method gets the value

Note : For more wonderful tutorials, please pay attention to the Triple design Tutorials section,

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.