A simple understanding of C #

Source: Internet
Author: User

Simple to understand. NET Framework

(1) First we need to know. NET Framework has two main components: Common language runtime CLR (Common Language Runtime) and framework class Libraries FCL (Framework class Library)

With the diagram:

(2). NET's compilation process:

With the diagram:

where the CLR also includes the common type system CTS (Common type systems) and the Common Language Specification CLS (Common Language specification)

(3). NET Framework Core class library and its functions

With the diagram:

(4)

The C # language needs to be compiled two times to program code that can be executed by CPU parsing:

First compilation: Compiling the C # language through the C # compiler into MSIL intermediate code

Second compilation: Compile the MSIL code through the instant compiler (JIT) to the CPU to parse the executed code, or become (platform-specific code)

Java programs that want to be executed by the CPU will also need to be compiled two times:

First compile: Compiles a file with the suffix Java compiler into a. Class (bytecode) file with a suffix called

Second compilation: Compile the. class file through a Java Virtual machine (JVM) into a code that the CPU can parse

(5) Object-oriented

Object-oriented three major features: encapsulation, inheritance, and polymorphism

The access modifiers we learned over the years:

Public: Any location, including cross-assembly

Internal: Can be accessed in the current assembly (project)

Private: Accessible only in the current class (in curly brackets of the current class)

Protected: can only be used in the current class and subclass

(6) Naming of variables, classes, and methods

Camel nomenclature: The first letter in lowercase, followed by the meaning of the first letter of the word capital. Variable

Pascal nomenclature: First letter capitalization class and method for each word

(7) New: Meaning of the Representative

1. Create an Object

2. Opening up space

3. Automatically call constructors

(8) Differences between fields and attributes

1. The property cannot save the data, it is the field that really holds the data.

2. The attribute is optional and the field is required.

3. If the field is only accessible within the class, then set to private, and if you want to find an intermediary that accesses a private field in the class in the main method, then the property can be an option.

(9) Automatic attributes

Definition: You can use automatic attributes when you don't need to make logical judgments for us. Defining automatic properties in the background automatically helps us to generate a private field for the corresponding data type

(ten) main uses of the CLR:

The CLR is the environment in which all. NET programs run, the Programming foundation for all. NET applications, and all. NET is governed and handled by the CLR, which includes in-process application loading, converting IL to GG,

Exception handling, garbage collection, loading assemblies

Drill down into C # data types

(1) value types and reference types

Value type: is the real value reference type stored on the stack: space on stacks and heaps

Value type: int double float Boolean char byte long Short enumeration (enum) struct body (struct)

Reference type: int[] class Interface (interface) string

(2) Structure

Defined:

Access modifier  struct struct  name {            //struct} example: public struct student{      //struct}

(2) Attention points:

1. There can be fields and methods in the structure

2. When defining a structure, the fields in the structure cannot be assigned an initial value

3. You can directly define the object of the structure without new

4. After declaring an object of a struct, you must assign an initial value to the members of the struct

5. Structs are also a special type of value

(3) Packing and unpacking

The process of converting a value type to a reference type is called boxing, which is called unpacking

Example:

static void Main (string[] args) {    int i=123;    Oject o=i;  Packing    i=456;   Change the content of I   Console.WriteLine ("Value of value type: {0}", i);    Console.WriteLine ("Value of reference type: {0}", O); }

(4) Different types of parameter transfer
Value Way parameter pass: The pass data type is a value type, no ref is not persisted in the method

The pass data type is a reference type, and no ref is permanently persisted in the method

Pass-by-reference: No ref will be kept forever

Example: Class voter{public   void Vote (se se)   {      //popular value +1      SE. popularity++;}    } /test related code se  zhang=new se (); Age=25;zhang. Name= "Zhang Mei", Zhang. Gender=gender.female;zhang. popularity=10;//before voting MessageBox.Show (Zhang. Sayhi ()); Voter voter=new voter (); voter. Vote (Zhang);  Reference type do parameter//vote after MessageBox.Show (Zhang. Sayhi ());   

(5) Differences in structure and class
1. A struct is a value type and a class is a reference type

2. Structure and another structure cannot inherit and be inherited

3. Structs have no default constructs and classes have

4. Structs do not have destructors and classes have

5. Structs may not be initialized with new, and classes cannot be

Use collections to organize related data

(1) Definition:

Collection:

Some specific objects that are set together are collections

Array:

A container that can store a heap of data of the same data type

(2) Collection

Collections are divided into generic and non-generic collections

Generic collections are divided into single-and double-column collections (List<t> and Dictionary<k,v>)

Non-generic collections are divided into single and double column collections (ArrayList and Hashtable)

(3) commonly used methods and properties

Add ();   Add Remove ();  Delete Remobeat ();  The number of the count//collection storage elements Contains () is not applicable according to the index deletion;  Detects if the element exists ContainsKey ();  Containsvalue (); Capcity//Collection Occupy space

Note: If you delete an element in the collection, the index of the collection is automatically maintained

(4) Traversal scheme:

ArrayList list=new ArrayList (); Student stu1=new Student (); Stu1. Name= "Jiejie"; Stu1. age=15; List. ADD (STU1); Student stu2=new Student (); Stu2. Name= "Jiji"; Stu2. age=18; List. ADD (STU2);  Foreachforeach (Student item in list) {    Console.WriteLine (item. Age+ "\ t" +item. Name);} forfor (int i=0;i<list. count;i++) {   Console.WriteLine ((Student) list[i]. Name);}

HashTable Traverse three scenarios   HashTable table=new HashTable (); The first way: Traverse all Keysforeach (var item in table. Keys) {    //an item represents a key   Console.WriteLine ("Key is {0}\tvalue is {1}", Item,table[item]);} The second traversal method: Iterates through all the value sets of foreach (var item in table. Values) {       //an item represents a value   Console.WriteLine ("Value is {0}", item);} Traverse the entire Tableforeach (DictionaryEntry item in table) {    Console.WriteLine ("Key is {0}\tvalue is {1}", item. Key,item. Value);}

Methods for in-depth classes

(1) Constructors

Method name and class name are the same, no return value

Note: In C #, when we define a class, the system's underlying default generates an parameterless construct with the same class name, but if we define a parameter construct, the system will not generate an parameterless construct, but the

In real development, we recommend that you define two constructors, a parameterless construct, and a parametric construct.

public class se{Public SE () {}public SE (string id,string name,int age,gender gender,int popularity) {   id=id;   Name=name;   Age=age;   Gender=gender;   popularity=popularity;}}

(2) Overloading of methods

In the same class, if there are more than one method method with the same name, but the number of arguments, the order, the type of the different methods can constitute the overload and the return value type of the method does not matter.

public void Say (String name,int age) {}public void Say (int age,string name) {}

(3) Interaction between objects

Remote control class public class remotecontrol{//Boot public void TurnOn (TV TV) {TV.  Open (); Call the TV object's power on Method}//Shutdown public void turnoff (TV TV) {TV.  Trunoff ();       }//Change station public void Changechannel (TV TV) {Console.WriteLine ("Please input channel:");       String Channelno=console.readline (); Tv.   Change (Channelno); }//TV type public class Tv{public boolean ison=false;//Turn on/off TV public void Open () {if (isOn) {Console.wri   Teline ("TV has been turned on");        } else {Console.WriteLine ("Successfully turned on the TV");     Isom=true;        }}//Shutdown public void turnoff () {if (isOn) {console.wrteline ("TV is shutting down");      Ison=false;     } else {Console.WriteLine ("TV is Off");    }}//Replace public void Change (string channelno) {if (isOn) {Console.WriteLine ("Cutting to {0} units", Channelno);   }}//Test Main () method static void Main (string[] args) {Remotecontrol controler=new remotecontrol ();   TV tv=new TV (); Boot controler.    TURNON (TV); Switch Channel Controler. ChAngchannel (TV); Shut down the machine controler.    Turnoff (TV); Console.ReadLine ();}

A simple understanding of C #

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.