如何在 ASP.NET 中设置自动实现属性的默认值

发布于 2024-12-18 08:45:59 字数 411 浏览 5 评论 0原文

我开始知道 C# 3.0 附带了自动实现属性的新功能,我喜欢它,因为我们不必在此声明额外的私有变量(与早期的属性相比),之前我使用的是属性,即

private bool isPopup = true;
public bool IsPopup
{
    get
    {
      return isPopup;
    }
    set
    {
      isPopup = value;
    }
}

现在我已将其转换为自动实现的属性,即

public bool IsPopup
{
    get; set;
}

我想将此属性的默认值设置为 true,甚至在 page_init 方法中也不使用它,我尝试过但没有成功,任何人都可以解释如何执行此操作吗?

I came to know that C# 3.0 comes with a new feature of Auto-Implemented Properties,I liked it as we don't have to declare extra private varible in this (compare to earlier property), earlier I was using a Property i.e.

private bool isPopup = true;
public bool IsPopup
{
    get
    {
      return isPopup;
    }
    set
    {
      isPopup = value;
    }
}

Now I've converted it into Auto-Implemented property i.e.

public bool IsPopup
{
    get; set;
}

I want to set the default value of this property to true without using it not even in page_init method, I tried but not succeeded, Can anyone explain how to do this?

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

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

发布评论

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

评论(5

擦肩而过的背影 2024-12-25 08:45:59

您可以在默认构造函数中初始化属性:

public MyClass()
{
   IsPopup = true;
}

使用 C# 6.0,可以像普通成员字段一样在声明处初始化属性:

public bool IsPopup { get; set; } = true;  // property initializer

现在甚至可以创建一个真正的只读自动属性,您可以直接初始化该属性,也可以在其中初始化该属性。构造函数,但未在类的其他方法中设置。

public bool IsPopup { get; } = true;  // read-only property with initializer

You can initialize the property in the default constructor:

public MyClass()
{
   IsPopup = true;
}

With C# 6.0 it is possible to initialize the property at the declaration like normal member fields:

public bool IsPopup { get; set; } = true;  // property initializer

It is now even possible to create a real read-only automatic property which you can either initialize directly or in the constructor, but not set in other methods of the class.

public bool IsPopup { get; } = true;  // read-only property with initializer
未蓝澄海的烟 2024-12-25 08:45:59

为自动属性指定的属性不适用于支持字段,因此默认值的属性不适用于此类属性。

但是,您可以初始化自动属性:

VB.NET

Property FirstName As String = "James"
Property PartNo As Integer = 44302
Property Orders As New List(Of Order)(500)

C# 6.0 及更高版本

public string FirstName { get; set; } = "James";
public int PartNo { get; set; } = 44302;
public List<Order> Orders { get; set; } = new List<Order>(500);

C# 5.0 及更低版本

不幸的是,6.0 以下的 C# 版本不支持支持这一点,因此您必须在构造函数中初始化自动属性的默认值。

Attributes specified for an auto property do not apply to the backing field, so an attribute for a default value won't work for this type of property.

You can, however, initialize an auto property:

VB.NET

Property FirstName As String = "James"
Property PartNo As Integer = 44302
Property Orders As New List(Of Order)(500)

C# 6.0 and above

public string FirstName { get; set; } = "James";
public int PartNo { get; set; } = 44302;
public List<Order> Orders { get; set; } = new List<Order>(500);

C# 5.0 and below

Unfortunately, C# versions below 6.0 do not support this, so you have to initialize the default values for auto properties in the constructor.

深海少女心 2024-12-25 08:45:59
using System.ComponentModel;

[DefaultValue(true)]
public bool IsPopup
{
    get
    {
      return isPopup;
    }
    set
    {
      isPopup = value;
    }
}
using System.ComponentModel;

[DefaultValue(true)]
public bool IsPopup
{
    get
    {
      return isPopup;
    }
    set
    {
      isPopup = value;
    }
}
衣神在巴黎 2024-12-25 08:45:59

您可以使用默认属性值,如下所示

此方法的一个优点是您不需要检查布尔值<的空值 /strong> 类型

using System.ComponentModel; 

public class ClassName
 {
   [DefaultValue(true)]
   public bool IsPopup{ get; set; }
 }

You can use default property value like below

One advantage of this method is you don't need to check null values for Boolean types

using System.ComponentModel; 

public class ClassName
 {
   [DefaultValue(true)]
   public bool IsPopup{ get; set; }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文