如何在构造非静态类对象之前在C#中初始化基本数据成员。

编程入门 行业动态 更新时间:2024-10-26 12:26:20
本文介绍了如何在构造非静态类对象之前在C#中初始化基本数据成员。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好C / C ++实现是:

Hello the C/C++ implementation is :

public class Person { int Id; Person(int a):Id(a) { } }

请有人能为我提供相同的C#实现 我尝试过: 我试过搜索谷歌,似乎在C#中我们没有这个选项。我想确认一下。

please could someone provide me with the C# implementation of the same What I have tried: I have tried searching google and it seem that in C# we do not have this option. I want to confirm this.

推荐答案

public class Person { int Id; Person(int a) { Id = a; } }

或this.Id = a取决于款式。

or "this.Id = a" depending on style.

>我想确认一下。 确认:将初始化方式放到构造函数中的方式如下: br $> b $ b >I want to confirm this. Confirmed: the way would be to put the initialisation to constructor like this: public class Person { public Person() { } public Person(int _id) { id = _id; } public int id { get; set; } }

更多推荐

如何在构造非静态类对象之前在C#中初始化基本数据成员。

本文发布于:2023-11-06 07:47:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   静态   对象   成员   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!