C # a simple review of common knowledge points (with pictures and truth)

Source: Internet
Author: User

1) value transfer call and Reference call Copy codeThe Code is as follows: using System;
Class MethodCall
{
Public static void Main ()
{
/*
* The parameter types include in, ref, and out. The default value is in.
* After the in type is modified in the sub-method, the value in the main method does not change.
* If the ref type is modified in the submethod, the value in the primary method also changes.
* The variables in the out main method do not need to be initialized.
*
*/
Int a = 1, B = 2, c;
Console. WriteLine ("Before Method Call: a = {0}, B = {1}, c is not assigned a value", a, B );
AMethod (a, ref B, out c );
Console. WriteLine ("After Method Call: a = {0}, B = {1}, c = {2}", a, B, c );
Console. Read ();
}
Public static void AMethod (int x, ref int y, out int z)
{
X = 110;
Y = 120;
Z = 119;
}
}

:

2) print triangle

Copy codeThe Code is as follows: using System;
Public class Hello
{
Public static void Main ()
{
Console. Write ("Enter the number of rows :");
Int lines = int. Parse (Console. ReadLine ());
Console. WriteLine ("");
For (int I = 1; I <= lines; I ++)
{
For (int k = 1; k <= lines-I; k ++)
Console. Write ("");
For (int j = 1; j <= I * 2 + 1; j ++)
Console. Write ("*");
Console. WriteLine ("");
}
Console. ReadLine ();
}
}

:

3) recursive factorial

Copy codeThe Code is as follows: using System;
Class Factor
{
Public static void Main ()
{
For (int I = 1; I <= 10; I ++)
Console. WriteLine ("the Factorial of {0} is {1}", I, Factorial (I ));
Console. Read ();
}
Public static long Factorial (long n)
{
If (n = 1)
Return 1;
Else
Return n * Factorial (n-1 );
}
}

:

4) polymorphism

Copy codeThe Code is as follows: using System;
Class Car
{
Public virtual void Drive ()
{Console. WriteLine ("Drive Car ");}
}
Class Truck: Car
{
Public override void Drive ()
{Console. WriteLine ("Drive Truck ");}
}
Class Client
{
Public static void Main ()
{
Car c = new Truck ();
C. Drive (); // polymorphism determines that the Drive method of Truck will be called.
Console. Read ();
}
}

:

5) method Overloading

Copy codeThe Code is as follows: using System;
Class Client
{
Public static void Main ()
{
// Reload indicates that the method name is the same and the method signature is different.
Console. WriteLine (Add (100,50 ));
Console. WriteLine (Add ("100", "50 "));
Console. Read ();
}
Public static string Add (string a, string B)
{
Return a + "add" + B;
}
Public static int Add (int a, int B)
{
Return a + B;
}
}

:

6) constructor

Copy codeThe Code is as follows: using System;
Public class Person
{
Public string name = "";
Public int age = 0;
// Default constructor
Public Person ()
{
}
// Overload the constructor (1)
Public Person (int Age)
{
This. age = Age;
}
// Overload the constructor (2)
Public Person (int Age, string Name)
{
This. age = Age;
This. name = Name;
}
Public void ShowInfo ()
{
Console. WriteLine ("name:" + name );
Console. WriteLine ("age:" + age );
}
}
Class Client
{
Public static void Main ()
{
Person p1 = new Person ();
P1.ShowInfo ();
Console. WriteLine ("*************************");
Person p2 = new Person (25 );
P2.ShowInfo ();
Console. WriteLine ("*************************");
Person p3 = new Person (25, "");
P3.ShowInfo ();
Console. Read ();
}
}

:

7) static and non-static

Copy codeThe Code is as follows: using System;
Class StaticHello
{
Public static void SayHello ()
{Console. WriteLine ("Static Hello ");}
}
Class NonStaticHello
{
Public void SayHello ()
{Console. WriteLine ("Non Static Hello ");}
}
Class Client
{
Public static void Main ()
{
// The "class name. Method" should be used for static method calls"
StaticHello. SayHello ();
// Use "Instance name. Method" for non-static method calls"
NonStaticHello h = new NonStaticHello ();
H. SayHello ();
Console. Read ();
}
}

:

8) Table 9

Copy codeThe Code is as follows: using System;
Public class JiuJiuBiao
{
Public static void Main (string [] args)
{
Int I, j;
For (I = 1; I <10; I ++)
{
For (j = 1; j <10; j ++)
{
Console. Write ("{0: D1} * {1: D1} = {2, 2}", I, j, I * j );
}
Console. WriteLine ("");
}
Console. ReadLine ();
}
}

:

9) bubble sort

Copy codeThe Code is as follows: using System;
Class ArraySort
{
Public static void Main ()
{
Int [] d = };
Int temp;
// Sort by bubble
For (int I = 0; I <d. Length; I ++)
For (int j = I + 1; j <d. Length; j ++)
If (d [I] <d [j])
{
Temp = d [I];
D [I] = d [j];
D [j] = temp;
}
// Output the sorting result
Foreach (int I in d)
Console. Write ("{0},", I );
Console. Read ();
}
}

:

10) Quantity of Quality

Copy codeThe Code is as follows: using System;
Class Factor
{
Public static void Main ()
{
For (int I = 1; I <= 100; I ++)
If (IsPrime (I ))
Console. WriteLine (I );
Console. Read ();
}
Public static bool IsPrime (int n)
{
For (int I = 2; I <= Math. Sqrt (n); I ++)
If (n % I = 0)
Return false;
Return true;
}
}

:

11) sort by API (1)

Copy codeThe Code is as follows: using System;
Using System. Collections;
Public class Person: IComparable
{
Public int ID;
Public string Rank;
Public Person (int id, string rank)
{This. ID = id; this. Rank = rank ;}
# Region IComparable Members
/*
* The IComparable interface has only one method: CompareTo. CompareTo Method
* Only one object type parameter is received, which means that it can receive any class
* Type data (the object is the parent class of all classes). This method returns
* Integer value, meaning:
*
* 1) the value is smaller than zero and the current instance (this) is smaller than the obj object.
* 2) equal to zero, the current instance (this) is equal to the obj object
* 3) the value is greater than zero. The current instance (this) is greater than the obj object.
*
* The IComparable interface has been implemented for Int32, int16..., String, Decimal, and other data types.
*/
Public int CompareTo (object obj)
{
Person p = (Person) obj;
Return this. ID. CompareTo (p. ID );
}
# Endregion
}
Class SortArrayList
{
Static void Main (string [] args)
{
ArrayList list = new ArrayList ();
List. Add (new Person (6, "Long queue "));
List. Add (new Person (3, "head "));
List. Add (new Person (4, ""));
List. Add (new Person (5, "brigade chief "));
List. Add (new Person (7, "persistent connections "));
List. Add (new Person (1, "military chief "));
List. Add (new Person (2, "battalion commander "));
List. Add (new Person (8, "teacher "));
List. Sort ();
Console. WriteLine ("After Sorting ");
Foreach (Person person in list)
{
Console. WriteLine ("ID:" + person. ID. ToString () + ", Rank:" + person. Rank );
}
}
}

:

12) sort by API (2)

Copy codeThe Code is as follows: using System;
Using System. Collections;
Public enum enuSortOrder
{IDAsc, IDDesc, RankAsc, RankDesc}
Public class Person: IComparable
{
Public static enuSortOrder intSortOrder = enuSortOrder. IDAsc;
Public int ID;
Public string Rank;
Public Person (int id, string rank)
{This. ID = id; this. Rank = rank ;}
# Region IComparable Members
/*
* The IComparable interface has only one method: CompareTo. CompareTo Method
* Only one object type parameter is received, which means that it can receive any class
* Type data (the object is the parent class of all classes). This method returns
* Integer value, meaning:
*
* 1) the value is smaller than zero and the current instance (this) is smaller than the obj object.
* 2) equal to zero, the current instance (this) is equal to the obj object
* 3) the value is greater than zero. The current instance (this) is greater than the obj object.
*
* The IComparable interface has been implemented for Int32, int16..., String, Decimal, and other data types.
*/
Public int CompareTo (object obj)
{
Person p = (Person) obj;
Switch (int) intSortOrder)
{
Case (int) enuSortOrder. IDAsc:
Return this. ID. CompareTo (p. ID );
Case (int) enuSortOrder. IDDesc:
Return p. ID. CompareTo (this. ID );
Case (int) enuSortOrder. RankAsc:
Return RankCompare (this. Rank, p. Rank );
Case (int) enuSortOrder. RankDesc:
Return RankCompare (p. Rank, this. Rank );
Default:
Return this. ID. CompareTo (p. ID );
}
}
Private int RankCompare (string rank1, string rank2)
{
Int intRank1 = ConvertRankToInt (rank1 );
Int intRank2 = ConvertRankToInt (rank2 );
If (intRank1 <intRank2)
Return-1;
Else if (intRank1 = intRank2)
Return 0;
Else
Return 1;
}
Private int ConvertRankToInt (string rank)
{
If (rank = "")
Return 8;
Else if (rank = "military chief ")
Return 7;
Else if (rank = "")
Return 6;
Else if (rank = "brigade chief ")
Return 5;
Else if (rank = "")
Return 4;
Else if (rank = "battalion commander ")
Return 3;
Else if (rank = "connection length ")
Return 2;
Else
Return 1;
}
# Endregion
}
Class SortArrayList
{
Static void Main (string [] args)
{
ArrayList list = new ArrayList ();
List. Add (new Person (6, "Long queue "));
List. Add (new Person (3, "head "));
List. Add (new Person (4, ""));
List. Add (new Person (5, "brigade chief "));
List. Add (new Person (7, "persistent connections "));
List. Add (new Person (1, "military chief "));
List. Add (new Person (2, "battalion commander "));
List. Add (new Person (8, "teacher "));
List. Sort ();
Console. WriteLine ("Sort By ID Asc :");
Foreach (Person person in list)
{
Console. WriteLine ("ID:" + person. ID. ToString () + ", Rank:" + person. Rank );
}
Console. WriteLine ("----------------------------");
Console. WriteLine ("Sort By ID Desc :");
Person. intSortOrder = enuSortOrder. IDDesc;
List. Sort ();
Foreach (Person person in list)
{
Console. WriteLine ("ID:" + person. ID. ToString () + ", Rank:" + person. Rank );
}
Console. WriteLine ("----------------------------");
Console. WriteLine ("Sort By Rank Asc :");
Person. intSortOrder = enuSortOrder. RankAsc;
List. Sort ();
Foreach (Person person in list)
{
Console. WriteLine ("ID:" + person. ID. ToString () + ", Rank:" + person. Rank );
}
Console. WriteLine ("----------------------------");
Console. WriteLine ("Sort By Rank Desc :");
Person. intSortOrder = enuSortOrder. RankDesc;
List. Sort ();
Foreach (Person person in list)
{
Console. WriteLine ("ID:" + person. ID. ToString () + ", Rank:" + person. Rank );
}
}
}

:

13) Attribute and method Scope

Copy codeThe Code is as follows: using System;
Class Base
{
/*
* Public is accessible to all classes.
* The private accessible range is the current class.
* The accessible scope of protected is the current class and its subclass.
*/
Public string name = "Tom ";
Private double salesary = 1500;
Protected int age = 20;
Public virtual void ShowInfo ()
{
Console. WriteLine (this. name); // yes, because name is public
Console. WriteLine (this. salary); // yes. salary is private and can be accessed in the Base class.
Console. WriteLine (this. age); // yes, because age is of the protected type, which can be accessed in the subclass.
}
}
Class Derived: Base
{
Public override void ShowInfo ()
{
Console. WriteLine (this. name); // yes, because name is public
// Console. WriteLine (this. salary); // No. salary is private and cannot be accessed when the Base is exceeded.
Console. WriteLine (this. age); // yes, because age is of the protected type, which can be accessed in the subclass.
}
}
Class Client
{
Public static void Main ()
{
Base B = new Base ();
Console. WriteLine (B. name); // yes, because name is public
// Console. WriteLine (this. salary); // No. salary is private and cannot be accessed when the Base is exceeded.
// Console. WriteLine (this. age); // No, because age is protected and Client is not a subclass of Base
Console. WriteLine ("========================= ");
B. ShowInfo ();
Console. WriteLine ("========================= ");
Derived d = new Derived ();
D. ShowInfo ();
}
}

:

15) fields and attributes

Copy codeThe Code is as follows: using System;
Class SumToHundred
{
Public static void Main ()
{
Int sum = 0;
For (int I = 1; I <= 100; I ++)
Sum + = I;
Console. WriteLine (sum );
}
}

PicCopy codeThe Code is as follows: using System;
Class Account
{
Private double balance = 0; // Field
Public double Balance // attribute
{
Get {return balance ;}
Set {balance = value ;}
}
/* ===================================================== ======================================
* The get and set methods can be modified to control access.
* Example:
*
* 1) read-only attributes
* Public double Balance // attribute
*{
* Get {return balance ;}
* Set {}
*}
*
* 2) read/write control
* Public double Balance
*{
* Get
*{
* If (Console. ReadLine () = "1234 ")
* Return balance;
* Else
* Returned-9999999;
*}
* Set {}
*}
* ===================================================== ======================================
*/
Public void Deposit (double n)
{This. balance + = n ;}
Public void WithDraw (double n)
{This. balance-= n ;}
}
Class Client
{
Public static void Main ()
{
Account a = new Account ();
A. Balance = 1000; // read/write attributes, because the property Balance is public
// A. balance = 1000; // fields cannot be read or written, because the field balance is private
A. WithDraw (500 );
A. Deposit (2000 );
Console. WriteLine (a. Balance );
}
}

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.