Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#数据类型

作者:月下孤魂   发布日期:2025-12-13   浏览:4

// 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}");

解释说明:

  1. 值类型:直接存储数据,包括基本数据类型(如 int, float, double, char, bool)和结构体(struct)。它们在栈中分配内存。
  2. 引用类型:存储对对象的引用,而不是对象本身。常见的引用类型有 string, object, 和自定义的类(class)。它们在堆中分配内存。
  3. 可空类型:允许值类型可以为 null,通过在类型后面加 ? 来表示。
  4. 枚举类型:用于定义一组命名的常量,使代码更具可读性。
  5. 结构体和类:结构体是值类型,而类是引用类型。两者都可以包含字段、属性和方法。

这段代码展示了 C# 中常见数据类型的使用方式,并附带了简单的输出语句来展示这些类型的值。

上一篇:c# 获取当前时间戳

下一篇:c# hashtable

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站