C#变量、值类型、引用类型

C#是一门由微软开发的编程语言。

对比 C/C++:C# 语法上借鉴了 C 和 C++ 的风格,但屏蔽了指针操作和手动内存管理,入门更安全,不容易犯内存相关的低级错误。

引用类型变量存储的是数据(实例)的引用,两个变量可以同时引用同一个对象;因此,对其中一个变量所做的操作,可能会影响到另一个变量所引用的对象

关键字 class interface delegate record 用于声明引用类型

C# 还提供了 dynamic object string 作为内置引用类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// using System;
namespace ConsoleApp1 // 名称空间:结构清晰、避免命名冲突
{
internal class Program // internal同程序集(dll、exe)访问权限
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!"); // 可以使用cw +Tab 快捷编码
Console.WriteLine("Hello, World!");
Console.Write("account:"); // 不换行
Console.Write("123456");
}
}
}

输出:

1
2
3
Hello, World!
Hello, World!
account:123456

Console是System名称空间下的,这个不需要显示的using System;

是因为已经隐士的使用了,右键项目—>【属性】—>【全局使用】

值类型

C# type keyword .NET type
bool System.Boolean
byte System.Byte
sbyte System.SByte
char System.Char
decimal System.Decimal
double System.Double
float System.Single
int System.Int32
uint System.UInt32
nint System.IntPtr
nuint System.UIntPtr
long System.Int64
ulong System.UInt64
short System.Int16
ushort System.UInt16

引用类型

C# type keyword .NET type
object System.Object
string System.String
delegate System.Delegate
dynamic System.Object
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
35
36
37
38
39
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
var list = new List<Dictionary<string, int>>();

int age = 20;

double pi = 3.1415926;

float distance = 23.124F;

decimal price = 19.99M;

bool isSunny = true;

char c = 'A';

string greeting = "Hello, World!";

Console.WriteLine(age);
Console.WriteLine(pi);
Console.WriteLine(distance);
Console.WriteLine(price);
Console.WriteLine(isSunny);
Console.WriteLine(c);

int a = 3;
int b = 5;
int result = a + b;
Console.WriteLine("a + b = result");
Console.WriteLine("{0} + {1} = {2}", a, b, result);
Console.WriteLine($"{a} + {b} = {result}"); // 常用
}
}
}

输出:

1
2
3
4
5
6
7
8
9
20
3.1415926
23.124
19.99
True
A
a + b = result
3 + 5 = 8
3 + 5 = 8

类类型

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
namespace ConsoleApp1
{
class Person
{
public int _age;
public string _name;

public static int s_maxAge = 150;
}

class Program
{
static void Main(string[] args)
{
Person p = new Person();
p._age = 1;
p._name = "Jerry";

// Person p1 = new Person();
Person p1 = new ();
p1._age = 3;
p1._name = "Tom";

Console.WriteLine($"{p._name} is {p._age} years");
Console.WriteLine($"{p1._name} is {p1._age} yesrs");
}
}
}

输出:

1
2
Jerry is 1 years
Tom is 3 yesrs

类方法

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
using System.Collections.Concurrent;

namespace ConsoleApp1
{
class Program
{
//static int Encrypt(int number)
//{
// return number * 2 + 4;
//}

// 表达式主体,语法糖: 简化代码写法
static int Encrypt(int number) => number * 2 + 4;

static int Decrypt(int number)
{
return (number - 4) / 2;
}

static void Show() => Console.WriteLine("show");

static void Main(string[] args)
{
Program program = new();

Show();
int qq = 123456;
int result = Encrypt(qq);
Console.WriteLine(result);
Console.WriteLine(Decrypt(result));
}
}
}

输出:

1
2
3
show
246916
123456

值类型 引用类型

引用类型变量存储的是数据(实例)的引用,两个变量可以同时引用同一个对象;因此,对其中一个变量所做的操作,可能会影响到另一个变量所引用的对象

关键字 class interface delegate record 用于声明引用类型

C# 还提供了 dynamic object string 作为内置引用类型

例1:两个变量引用同一对象

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
namespace ConsoleApp1
{
class Person
{
public Person(string name, int age)
{
_name = name;
_age = age;
}
public string _name;
public int _age;
}

class Program
{
static void Main(string[] args)
{
Person p1 = new ("Tom", 7);
Person p2 = p1;

p2._name = "Jerry";

Console.WriteLine($"{p1._name} is {p1._age} years old");
Console.WriteLine($"{p2._name} is {p2._age} years old");
}
}
}

输出:

1
2
Jerry is 7 years old
Jerry is 7 years old

例2:

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
35
36
37
38
namespace ConsoleApp1
{
class Person
{
public Person(string name, int age)
{
_name = name;
_age = age;
}
public string _name;
public int _age;
}

class Program
{
// 引用
static void SetAge(Person p, int newAge)
{
p._age = newAge;
}
// 值
static void SetInt(int i, int newVale)
{
i = newVale;
}

static void Main(string[] args)
{
Person p1 = new ("Tom", 7);
SetAge(p1, 70);
Console.WriteLine(p1._age);

int v = 1024;
SetInt(v, 4096);
Console.WriteLine(v);
}
}
}

输出:

1
2
70
1024

资料查找:C# 语言参考 - C# reference | Microsoft Learn

https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/

study from: 2026年 C#零基础入门教程_哔哩哔哩_bilibili