C # programming Basics

Source: Internet
Author: User
Tags case statement

Introduction to. NetFramework
. Netframework runs on the operating system and provides good cross-language features.
. Netframework contains two content: Common Language Runtime (CLR) and Class Library Set (FCL)
MSIL is an intermediate language of Microsoft. When compiling code in a language supported by. net, the output code is MSIL.
CLR also includes: general language specifications (CLS: a set of rules to ensure language interoperability) and Public type systems (CTS: includes.. net supports data types and functions compatible with various languages)

Variables and constants in C #
C # basic data types:
Value Type and reference type
Value Type: simple type, structure type, and enumeration type.
Simple Type: integer, floating point, decimal, and Boolean
Sbyte is signed and equivalent to the byte type of java, range:-128 ~ 127
In C #, byte is unsigned and ranges from 0 ~ 255
Variable naming method:
Pascal naming and camel naming
Pascal's naming method: if multiple words are composed, each letter is capitalized.
Camel naming: if multiple words are in the same name, the first word is in lowercase, and the subsequent words are in uppercase.
Constants in C #: const and readonly
A constant declared by const: a static constant. It is initialized only when it must be declared and can only be initialized using a constant value.
A constant declared by readonly: a dynamic constant. It can be declared without initialization. It can only be initialized in the constructor, but must be initialized in each constructor. It can also be initialized using variable values.

Class Test
{
Const float PI = 3.1416f; // constant name: all uppercase
Readonly float G;
Public Test ()
{
G = 9.80F;
}
Public Test (float g) // each constructor must be initialized. You can also use variable values for initialization.
{
G = g;
}
}

Packing: Convert the value type to a reference type.
Binning: converts a reference type to a value type.
Value Type: in the stack
Reference Type: The address (reference) of the object stored in the heap. the object itself is stored in the stack.
Handling of allowed value types and reference types in the disassembly box

C # syntax




C # packing and unpacking

Switch () can be int, char, and string in brackets. The case statement in the switch statement does not write anything after the colon. break can be left blank. In other cases, you must write break. Otherwise, an error is reported.

Array: Five declaration Methods
Int [] array;
Array = new int [2];

// Method 2
Int [] array1 = new int [2];

// Method 3
Int [] array2 = {1, 2, 3 };

// Method 4
Int [] array3 = new int [] {1, 2, 3 };

// Method 5
Int [] array4 = new int [3] {1, 2, 3 };

// The array size can also be a variable
Int count = 3;
Int [] arr = new int [count];


Enumeration: Use meaningful characters to access data
Public enum Contry: long // specifies the enumeration type, which must be an integer and is not written to the int type.
{
Pacpacific, // The first unpaid value. The default value is zero.
China = 1860,
Japan,
S = 1901,
Canada

}


Object-oriented in C #
Destructor: The function name is the same as the constructor name ,~ Function Name (). parameters are not accepted and are automatically called by the garbage collector (GC. Collect)

Virtual Keyword: in C #, to override the method of the parent class, the method of the parent class must be identified as virtual (virtual), and the override method must be modified with override.
New Keyword: A new method defined in the subclass is the same as the signature of the parent method. It is not a method to override the parent class.
Base Keyword: Use the base keyword to call the method of the parent class

Access modifier:
Public,
Internal in a project,
Protected Parent-Child class,
Private is only a member of the class.

Note: If a class inherits both classes and implements interfaces, the class name must be written before the interface name.


Attribute, indexer, Delegate, event

Attribute: the access modifier is generally public, and the first letter is capitalized. There must be get and set accessors in the property, get must have return, and set must have the value keyword, representing the value accepted from the outside.
Indexer: The Role of Indexer: Process class objects like arrays.
Public class Student
{
Private string [] obj = new string [10];

// This keyword indicates the object of each class. The integer in [] indicates accessing through subscript.
Public string this [int index] // this is a member of each class that can be accessed through the 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] = "Xiao wulang ";
Stucollection [5] = "";
}
}


Delegate: equivalent to a function pointer, which allows the program to specify the method to run when running.
(1) define the delegate: Public delegate int Call ()
(2) instantiate the delegate: objcall = new Call (method name)
(3) Call delegate: objcall ();

Event: an event is actually a Special Delegate. A delegate can only point to one method at a time, and an event can point to multiple methods.
(1) define a delegate public delegate void delegateMe ();
(2) define an event private event delegate eventMe;
(3) Subscription event eventMe + = new delegateMe (method name 1 ());
EventMe + = new delegateMe (method name 2 ());
(4) trigger event if (condition) then eventMe ();

Multithreading

Create a thread instance:
Thread obj = new Thread (new ThreadStart (method name ))
Start: Start ();
Sleep ();
Termination: Abort ();
Pending: Suspend ();
Restore: Resume ();
Current Thread: Thread. CurrentThread
ThreadPriority enumeration value is used to specify the priority of the scheduling thread (a total of 5 levels)

Lock keyword
Use the lock keyword in C # To provide Synchronization
Thread Synchronization: when different threads access shared resources, only one thread can access the resources at a time.
Lock (this)
{
For (int I = 0; I <10; I ++)
{
// Statement;
}
}

Array set object

Array: Array is very similar to the Array, and can be converted and copied to each other. Many methods are common. You can use the static method of Array to reverse and sort the Array. This is not possible for the Array itself.

Using system. Array;
Array ar = Array. CreateInstance (typeof (int), 5); // create an Array instance
Ar. SetValue (12, 0); // value assignment

Array. Reverse (ar); // Reverse
Array. Sort (ar); // Sort

ArrayList: One of the most common collections. The benefit of a set is that it can automatically increase the capacity without knowing the data size, but the array cannot.
Add () Add element
Remove (location) Remove Element

ArrayList al = new ArrayList ();
// If You Want to traverse the collection elements,
// Method 1:
// Copy the number in the set to the array
Object [] temp = al. ToArray ();
Foreach (object t in temp)
{
Console. WriteLine (t );
}

// Method 2: Use the iterator
IEnumerator ie = al. GetEnumerator ();
While (ie. MoveNext ())
{
Console. WriteLine (ie. Current );
}

HashTable: Save the value as a key-Value Pair

Hashtable hash = new Hashtable (4 );
Hash. Add ("", 1860 );
Hash. Add ("", 1940 );

Console. WriteLine (hash ["China"]. ToString (); obtain value through key


SortedList: a mixture of Hashtable and Array
Can store key-value pairs, similar to Hashtable
It can be directly traversed through indexes through its own methods, similar to Array
Objsortlist. GetKey (I) method to obtain the key
Objsortlist. GetByIndex (I) method to obtain the value

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.