C # learning,
C # getting started with development (MOOC)
1. Introduction to C #
1-1. NET Introduction
The. NET platform can be programmed in multiple languages, and C # is the best combination.
1-2 Introduction and installation of Visual Studio
...
1-6 Hello World
Console program: Main method, program entry
Console. Write ("Hello World \ n"); // Ctrl + F5 run directly
Namepalce namespace -- class (C # A program consists of classes)
2. C # syntax Basics
Const constant
Array declaration:
String [] name = new string [3]; char [] a = new char [] {'A', 'B', 'C '}; char [] a = {'A', 'B', 'C'}; // two-dimensional array, which is considered as an array int [,] arr = new int [2, 3]; // contains two one-dimensional arrays, each of which contains three variables, a total of 2*3 = 6 array elements
Foreach
The foreach loop can read every array element regardless of the index, meaning iteration.
int[] num = {1, 2, 3};foreach(int i in num){ Console.Write(i);}
Receive input in the console:
string name;name = Console.Readline();
Perform breakpoint tracking and debugging in C:
F9: Set/cancel a breakpoint
F5 debugging
F10 for one-step execution
F11 entry method body
Use C # For encapsulation (MOOC)
1. Classes and objects in C #
1-1 concepts of classes and objects:
Object-oriented, encapsulation, inheritance, polymorphism, class, object, field (attribute), method.
Instantiation
1-2 Definition class
Right-click the project and choose add class.
Namespace MyFirstApp {// <summary> /// kids // </summary> class Child {public string name; public int age; public void playBall () {Console. write ("I am LBJ ");}}}
1-3 instantiate the object and access the object Member
Child XiaoMIng = new Child (); // instantiate the object XiaoMing. name = "James"; // assign XiaoMIng. playBall () to the field; // CALL THE METHOD
1-4 access modifier public & private
Public and private)
The default value is private.
2. encapsulation of attributes
2-1 encapsulation Concept
Why use encapsulation in a program-use encapsulation (hiding object information and setting aside access interfaces)
Class Child {private string _ sex; public string Sex {// get, set, and value are all keywords get {return _ sex ;} // set {_ sex = value;} // write accessors }}
2-2 code to encapsulate attributes
Encapsulated shortcut: Ctrl + R + E
Class Child {private string _ name; // use the shortcut key Ctrl + R + E to quickly encapsulate public string Name {get {return _ name ;} set {_ name = value ;}}}
2-3 read-only attributes
Only the get accessors are available, and fields are initialized using the constructor.
2-4 assign values through the set block constraint attribute
Private int _ age; public int Age {get {return _ age;} set {// constraint attribute value assignment if (value> 3 & value <7) _ age = value ;}}
3. Method
3-1 Define and call the method without Parameters
...
3-2 define and call the method with Parameters
Real Parameters
Public void EatSuge (string suger) {if (suger = "durian") // output else // output}
3-3 method overloading (determined by the difference between the real parameter type and the number of parameters)
public void EatSuge(int count){ //... }public void EatSuge(string suger, int count){ //... }
3-4 call the end method through return or return the return value
4. Constructor
4-1 default constructor, no-argument constructor, and parameter Constructor
Constructor: assign an initial value to the property when instantiating an object.
It is usually declared as public and can be reloaded.
No parameter:
public Child(){//...}
Parameters:
public Child(string name, string sex, int age){_name = name;_sex = sex;_age = age;}
4-2 this keyword
This indicates the current object
public Child(string name, string sex, int age){this._name = name;this._sex = sex;this._age = age;}
4-3 object initializer
Child child = new Child(){Name = "asf", Age = 5};
5. Value Type and reference type
5-1 Value Type and reference type
** Value types: int, char, double, and bool
// The value of x remains unchanged, and the value of the y copy is created. // The value of the Value Type Variable storage object will create a copy worth the value. modifying any copy will not affect the value of other replicas int x; int y = x; y --; // The value of y remains unchanged.
** Reference type: Class (string), array, and interface
// The memory address of the reference type storage object // The Value assignment does not create a copy worth it, but only creates a copy of the address // modifies any copy, also modify other replicas Child c1 = new Child (); c1.Name = "Zhang Xiaoming"; Child c2 = c1; c2.Name = "Liu Xiaoyong"; Console. write (c1.Name); // output is Liu Xiaoyong Console. write (c2.Name); // The output is Liu Xiaoyong
5-2 structure (value type, including fields, attributes, and methods)
The structure and class are different:
The most fundamental difference is that the structure is the value type, and the class is the reference type;
The structure field cannot be assigned an initial value;
The structure cannot define a constructor without parameters;
The constructor in the structure must assign values to all fields;
Structure definition is not required;
5-3 enumeration (value type)
For example, use enumeration to restrict the assignment of sex
Enum Gender {male, female}
Features:
Enumeration is a value type;
Fields, attributes, and methods cannot be defined for enumeration;
The enumerated value is an integer that increases from 0.
5-4 The Value Type and reference type in the parameter passed by the method
Differences between the value type and the value of the reference type
The reference type. parameters are passed by reference. The address is passed, which leads to changes in the form parameter.
Value Type: Pass the parameter by value, and pass the value copy-the form parameter is changed, and the real parameter remains unchanged.
5-5 ref keywords
// Enable the parameter of the value type to pass the class Program {static void Main (string [] args) {int age = 2; increase (ref age) according to the reference ); // input age reference, similar to C ++'s & age Console. write (age);} static void increase (ref int Age) // ref int incoming reference, similar to the int of C ++ & {Age ++ ;}}
5-6 out keywords
Returns multiple values.
Example:
Class Program {static void Main (string [] args) {int ly, ny; // last year's age, next year's age int age = 2; lyny (age, out ly, out ny); // returns the age of last year and the age of next year. writeLine (ly + "" + ny); // output 1 3} static void lyny (int Age, out int lastyear, out int nextyear) // output last year's Age and next year's Age {lastyear = Age-1; nextyear = Age + 1 ;}}
5-7 course Summary
Encapsulation
Class and Object
Encapsulate fields into attributes
Method
Constructor
Value Type and reference type