c# 结构体与类的差别
2025-01-27 09:10 钟铧若岩 阅读(11) 评论(0) 编辑 收藏 举报类型本质
声明与使用示例
// 结构体定义 struct PointStruct { public int X; public int Y; public PointStruct(int x, int y) { X = x; Y = y; } } // 类定义 class PointClass { public int X; public int Y; public PointClass(int x, int y) { X = x; Y = y; } } class Program { static void Main() { // 结构体使用 PointStruct struct1 = new PointStruct(1, 2); PointStruct struct2 = struct1; struct2.X = 3; // struct1.X 仍为 1,因为是值类型复制 Console.WriteLine($"struct1.X: {struct1.X}, struct2.X: {struct2.X}"); // 类使用 PointClass class1 = new PointClass(1, 2); PointClass class2 = class1; class2.X = 3; // class1.X 变为 3,因为引用同一对象 Console.WriteLine($"class1.X: {class1.X}, class2.X: {class2.X}"); } }
构造函数
继承性
成员修饰符
应用场景
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南