C # basic knowledge,
Part from C # In Shell
1. What is C #?
C # is a common type-safe and object-oriented programming language.
Type security: types can only interact with each other through the protocols they define to ensure internal consistency of one type. C # supports static typing and performs static type security check during compilation.
Object-oriented features: encapsulation, inheritance, and polymorphism.
2.. Net Framework
. Net Framework is composed of CLR and a large number of libraries. CLR is the runtime environment for executing hosted code.
3. Value Type and reference type
Value types include most built-in types, custom struct types, and enum types. Copying a value type always copies an instance.
Sample Code:
1 // define the structure Product 2 public struct Product {public int price, double discount;} 3 4 5 static void Main () 6 {7 Product productOne = new Product (); 8 productOne. price = 12; 9 10 Product productTwo = productOne; 11 12 Console. writeLine (productOne. price); // 1213 Console. writeLine (productTwo. price); // 1214 15 productOne. price = 10; 16 17 Console. writeLine (productOne. price); // 1018 Console. writeLine (productTwo. price); // 1219}View Code
Storage:
ProductOne and productTwo are stored in different memory spaces.
Reference types include class, Data, delegate, and interface. The reference type contains references of objects and objects. assigning values to the reference type only copies references, not object instances.
Sample Code:
Public class Product {public int price, int discount;} static void Main () {Product productOne = new ProductOne (); productOne. price = 12; Product productTwo = productOne; Console. writeLine (productOne. price); // 12 Console. writeLine (productTwo. price); // 12 ProductOne. price = 10; Console. writeLine (productOne. price); // 10 Console. writeLine (productTwo. price); // 10}View Code
Storage:
4. C # statements
Select Statement (if, switch)
If-else example:
if(1+1 == 2) Console.WriteLine("true");else Console.WriteLine("false");
Switch example:
switch(number){ case 1: Console.WriteLine("One"); break; case 2: Console.WriteLine("Two"); break; default: Console.WriteLine(number); break;}
Loop statement (for, foreach, while, do-while)
For Loop sample code:
for(int i=1;i<=10;i++){ Console.Write(i);}
Example code of foreach loop:
foreach(char c in "code"){ Console.WriteLine(c); }
Difference between while and do-while:
The while loop is checked before execution, and the do-while loop is executed at least once before the check.
Sample Code:
int i=0;while(i<=10){ Console.WriteLine(i); i++;}int j=0;do{ Console.WriteLine(j);
j++;
}while(j<=10)