0%

C#

数字

类型 范围 大小 .NET 类型
sbyte -128 到 127 signed 1 byte System.SByte
byte 0 到 255 unsigned 1 byte System.Byte
short -32,768 到 32,767 signed 2 byte System.Int16
ushort 0 到 65,535 unsigned 2 byte System.UInt16
int -2,147,483,648 到 2,147,483,647 signed 4 byte System.Int32
uint 0 到 4,294,967,295 unsigned 4 byte System.UInt32
long -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 signed 8 byte System.Int64
ulong 0 到 18,446,744,073,709,551,615 unsigned 8 byte System.UInt64
float ±1.5 x 10−45 至 ±3.4 x 1038 4 byte System.Single
double ±5.0 × 10−324 到 ±1.7 × 10308 8 byte System.Double
decimal ±1.0 x 10-28 至 ±7.9228 x 1028 16 byte System.Decimal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int a = 18;
int b = 6;
int c = a + b;
int d = (a + b) / c;
int e = (a + b) % c;
int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine(c);
Console.WriteLine($"quotient: {d}"); //quotient: 3
Console.WriteLine($"remainder: {e}"); //remainder: 2

double a = 5;
double b = 4;
double c = 2;
double d = (a + b) / c;
double max = double.MaxValue; //-1.79769313486232E+308
double min = double.MinValue; //1.79769313486232E+308

// its precision is greater than double
// but the range of decimal is smaller
decimal a = 1.0M;
decimal b = 3.0M;
decimal min = decimal.MinValue; //-79228162514264337593543950335
decimal max = decimal.MaxValue; //79228162514264337593543950335

分支与循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(a == b){

}else{

}

while(){

}

do{

}while();

for(int i = 0; i < 10; ++i){

}

泛型列表类型管理数据集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var names = new List<string> { "<name>", "Ana", "Felipe" };
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

// operation
Console.WriteLine();
names.Add("Maria");
names.Add("Bill");
names.Remove("Ana");
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

// index
Console.WriteLine($"My name is {names[0]}.");
Console.WriteLine($"I've added {names[2]} and {names[3]} to the list.");
var index = names.IndexOf("Felipe");
if (index != -1)
Console.WriteLine($"The name {names[index]} is at index {index}");
var notFound = names.IndexOf("Not Found");
Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");

// length
Console.WriteLine($"The list has {names.Count} people in it");

// sort
names.Sort();
foreach (var name in names)
{
Console.WriteLine($"Hello {name.ToUpper()}!");
}

类与接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class Point3D: Point
{
public int z;
public Point3D(int x, int y, int z) :
base(x, y)
{
this.z = z;
}
}
interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
interface IComboBox: ITextBox, IListBox {}

数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int[] a = new int[10];
for (int i = 0; i < a.Length; i++){
Console.WriteLine($"a[{i}] = {a[i]}");
}

//Multidimensional array
int[] a1 = new int[10];
int[,] a2 = new int[10, 5];
int[,,] a3 = new int[10, 5, 2];

int[][] a = new int[3][];
a[0] = new int[10];
a[1] = new int[5];
a[2] = new int[20];

int[] a = {1, 2, 3};
int[] a = new int[] {1, 2, 3};

STL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// List
List<String> list = new List<String>();
list.Add(i.ToString());

// LinkedList
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.AddLast(i.ToString());

// Set
HashSet<int> set = new HashSet<int>();
set.Add(1);
set.Clear();
set.Remove(1);
set.Contains(1);
set.Equals(Object o);

// Map(Dictionary)
Dictionary<int, int> dic = new Dictionary<int, int>();
dic.Add(1, 2);
dic.Remove(1);
dic.ContainsKey(1);
int val = dic[1];
Dictionary<int, int>.ValueCollection ValueSet = dic.Values;

foreach(KeyValuePair<int, int> kv in dic){
// kv.Key, kv.Value
}

string s = string.Empty;
if(dic.TryGetValue(1, out s)){
// success
}else{
// fail
}

Reference

https://docs.microsoft.com/zh-cn/dotnet/csharp/