// C# 数据类型示例
// 值类型 (Value Types)
int integer = 10; // 整数类型
float floatingPoint = 3.14f; // 单精度浮点数
double doublePrecision = 3.141592653589793; // 双精度浮点数
char character = 'A'; // 字符类型
bool boolean = true; // 布尔类型
// 引用类型 (Reference Types)
string text = "Hello, World!"; // 字符串类型
object obj = new object(); // 对象类型
// 可空类型 (Nullable Types)
int? nullableInt = null; // 可以为 null 的整数类型
bool? nullableBool = null; // 可以为 null 的布尔类型
// 枚举类型 (Enum Types)
enum DayOfWeek { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };
DayOfWeek today = DayOfWeek.Monday;
// 结构体 (Struct)
struct Point
{
public int X;
public int Y;
}
Point point = new Point { X = 0, Y = 0 };
// 类 (Class)
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person person = new Person { Name = "John Doe", Age = 30 };
// 输出各种类型的值
Console.WriteLine($"Integer: {integer}");
Console.WriteLine($"Floating Point: {floatingPoint}");
Console.WriteLine($"Double Precision: {doublePrecision}");
Console.WriteLine($"Character: {character}");
Console.WriteLine($"Boolean: {boolean}");
Console.WriteLine($"String: {text}");
Console.WriteLine($"Object: {obj}");
Console.WriteLine($"Nullable Int: {nullableInt}");
Console.WriteLine($"Nullable Bool: {nullableBool}");
Console.WriteLine($"Enum: {today}");
Console.WriteLine($"Struct: ({point.X}, {point.Y})");
Console.WriteLine($"Class: {person.Name}, {person.Age}");
int, float, double, char, bool)和结构体(struct)。它们在栈中分配内存。string, object, 和自定义的类(class)。它们在堆中分配内存。null,通过在类型后面加 ? 来表示。这段代码展示了 C# 中常见数据类型的使用方式,并附带了简单的输出语句来展示这些类型的值。
上一篇:c# 获取当前时间戳
下一篇:c# hashtable
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站