如何在构造非静态类对象之前在C#中初始化基本数据成员。 [英] How to initialize a primitive data member in C# before construction of a non static class object.

查看:55
本文介绍了如何在构造非静态类对象之前在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#中初始化基本数据成员。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆