如何在 C# 自动属性中返回对象的新实例

发布于 2024-08-02 09:16:41 字数 394 浏览 2 评论 0原文

是否可以使用 C# 自动属性创建对象的新实例?

在 C# 中,我喜欢如何做到这一点:

public string ShortProp {get; set;}

是否可以对像 List 这样首先需要实例化的对象执行此操作?

IE:

List<string> LongProp  = new List<string>(); 
public List<string> LongProp {  
    get {  
        return LongProp ;  
    }  
    set {  
         LongProp  = value;  
    }  
}

Is it possible, using C# Automatic Properties, to create a new instance of an object?

in C# I love how I can do this:

public string ShortProp {get; set;}

is it possible to do this for objects like List that first need to be instantiated?

ie:

List<string> LongProp  = new List<string>(); 
public List<string> LongProp {  
    get {  
        return LongProp ;  
    }  
    set {  
         LongProp  = value;  
    }  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彩扇题诗 2024-08-09 09:16:41

您可以在构造函数上初始化您的支持字段:

public class MyClass {
    public List<object> ClinicalStartDates {get; set;}
    ...
    public MyClass() {
        ClinicalStartDates = new List<object>();
    }
}

但是...您确定要将此列表设为具有公共设置器的属性吗?我不知道你的代码,但也许你应该公开更少的类属性:

public class MyClass {
    public List<object> ClinicalStartDates {get; private set;}
    ...
    public MyClass() {
        ClinicalStartDates = new List<object>();
    }
}

You can initialize your backing field on the constructor:

public class MyClass {
    public List<object> ClinicalStartDates {get; set;}
    ...
    public MyClass() {
        ClinicalStartDates = new List<object>();
    }
}

But... are you sure about making this list a property with a public setter? I don't know your code, but maybe you should expose less of your classes' properties:

public class MyClass {
    public List<object> ClinicalStartDates {get; private set;}
    ...
    public MyClass() {
        ClinicalStartDates = new List<object>();
    }
}
抹茶夏天i‖ 2024-08-09 09:16:41

您必须在构造函数中初始化它:

class MyClass {

  public MyClass() {
    ShortProp = "Some string";
  }

  public String ShortProp { get; set; }

}

You will have to initialize it in the constructor:

class MyClass {

  public MyClass() {
    ShortProp = "Some string";
  }

  public String ShortProp { get; set; }

}
心的位置 2024-08-09 09:16:41

我从来不想在类定义中声明变量。在构造函数中执行它。

因此,按照该逻辑,您可以在类的构造函数中 inlitailize ShortProp。

I never want to declare a variable in the class defination. Do it in the constructor.

So following that logic, you can inlitailze your ShortProp in the constructor of your class.

四叶草在未来唯美盛开 2024-08-09 09:16:41

唯一可以做到这一点的方法是在构造函数中。


public List<ClinicalStartDate> ClinicalStartDates { get; set; }

public ObjCtor()
{
   ClinicalStartDates = new List<ClinicalStartDate>;
}

但这可能不是您想听到的。

The only way you can do this, is in constructor.


public List<ClinicalStartDate> ClinicalStartDates { get; set; }

public ObjCtor()
{
   ClinicalStartDates = new List<ClinicalStartDate>;
}

That's probably not what you wanted to hear though.

雾里花 2024-08-09 09:16:41

C# 3.5 不支持自动属性的自动初始化
所以是的,您应该使用构造函数
您的问题重复

C# 3.5 does not support automatic initialization of automatic properties.
So yes, you should use a constructor.
And your question is a duplicate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文