标签: automatic-properties

如何为C#Auto-Property提供默认值?

如何为C#Auto-Property提供默认值?我要么使用构造函数,要么还原为旧语法.

使用构造函数:

class Person 
{
    public Person()
    {
        Name = "Initial Name";
    }
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

使用普通属性语法 (使用默认值)

private string name = "Initial Name";
public string Name 
{
    get 
    {
        return name;
    }
    set
    {
        name = value;
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的办法?

c# automatic-properties

1773
推荐指数
16
解决办法
78万
查看次数

公共字段与自动属性

我们经常被告知我们应该通过为类字段制作getter和setter方法(C#中的属性)来保护封装,而不是将字段暴露给外部世界.

但是很多时候,一个字段只是用来保存一个值,并且不需要任何计算来获取或设置.对于这些,我们都会这样做:

public class Book
{
    private string _title;

    public string Title
    {
          get{ return _title;  }
          set{ _title = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

好吧,我有一个坦白,我不忍心写所有这些(真的,它不必写它,它不得不看它),所以我去流氓和使用公共领域.

然后是C#3.0,我看到他们添加了自动属性:

public class Book
{
    public string Title {get; set;} 
}
Run Code Online (Sandbox Code Playgroud)

这更整洁,我很感激它,但是真的,除了创建一个公共领域之外还有什么不同?

public class Book
{
    public string Title;
}
Run Code Online (Sandbox Code Playgroud)

c# field properties class automatic-properties

333
推荐指数
10
解决办法
6万
查看次数

初始化C#自动属性

我习惯写这样的课程:

public class foo {
  private string mBar = "bar";
  public string Bar {
    get { return mBar; }
    set { mBar = value; }
  }
  //... other methods, no constructor ...
}
Run Code Online (Sandbox Code Playgroud)

将Bar转换为自动属性看起来既方便又简洁,但是如何在不添加构造函数并将初始化放在那里的情况下保留初始化?

public class foo2theRevengeOfFoo {
  //private string mBar = "bar";
  public string Bar { get; set; }
  //... other methods, no constructor ...
  //behavior has changed.
}
Run Code Online (Sandbox Code Playgroud)

您可以看到添加构造函数并不符合我应该从自动属性中获得的省力.

这样的事情对我来说更有意义:

public string Bar { get; set; } = "bar";
Run Code Online (Sandbox Code Playgroud)

c# initialization automatic-properties

180
推荐指数
3
解决办法
15万
查看次数

C#3.0自动属性 ​​- 有用与否?

注意:这是在我开始使用C#时发布的.凭借2014年的知识,我可以真正地说,自动属性是C#语言中发生过的最好的事情.

我习惯使用私有和公共字段在C#中创建我的属性:

private string title;
public string Title
{
    get { return title;  }
    set { title = value;  }
}
Run Code Online (Sandbox Code Playgroud)

现在,使用.NET 3.0,我们获得了自动属性:

public string Title { get; set; }
Run Code Online (Sandbox Code Playgroud)

我知道这更像是一个哲学/主观问题,但除了为每个字段保存五行代码之外,是否有任何理由使用这些自动属性?我个人的抱怨是那些属性隐藏了我的东西,我不是黑魔法的忠实粉丝.

事实上,隐藏的私有字段甚至没有显示在调试器中,这是好的,因为get/set函数什么都不做.但是当我想实际实现一些getter/setter逻辑时,我必须使用私有/公共对.

我看到了我节省大量代码(一对六行)而不会失去以后更改getter/setter逻辑的能力的好处,但是我再次通过简单地声明一个公共字段"Public string Title"就可以做到这一点需要{get; 组; 阻止,因此甚至可以节省更多代码.

那么,我在这里错过了什么?为什么有人真的想要使用自动属性?

.net c# automatic-properties

152
推荐指数
10
解决办法
4万
查看次数

C#3.0+中属性和字段的区别

我意识到它似乎是C#中字段和属性之间有什么区别的重复但我的问题略有不同(从我的观点来看):

一旦我知道了

  • 我不会将我的课程用于"仅适用于属性的技术"和
  • 我不会在getter/setter中使用验证代码.

是否有任何区别(风格/未来发展除外),如设置属性时的某种控制类型?

是否有任何额外的区别:

public string MyString { get; set; }
Run Code Online (Sandbox Code Playgroud)

public string myString;
Run Code Online (Sandbox Code Playgroud)

(我知道,第一个版本需要C#3.0或更高版本,并且编译器会创建私有字段.)

c# field properties automatic-properties c#-3.0

138
推荐指数
6
解决办法
7万
查看次数

C#Lazy Loaded自动属性

在C#中,

有没有办法将自动属性转换为具有指定默认值的延迟加载自动属性?

基本上,我试图扭转这个......

private string _SomeVariable

public string SomeVariable
{
     get
     {
          if(_SomeVariable == null)
          {
             _SomeVariable = SomeClass.IOnlyWantToCallYouOnce();
          }

          return _SomeVariable;
     }
}
Run Code Online (Sandbox Code Playgroud)

到不同的东西,我可以指定默认值,它自动处理其余的...

[SetUsing(SomeClass.IOnlyWantToCallYouOnce())]
public string SomeVariable {get; private set;}
Run Code Online (Sandbox Code Playgroud)

c# automatic-properties

93
推荐指数
6
解决办法
4万
查看次数

自动属性和结构不混合?

在回答这篇文章的同时踢了一些小结构,我意外地遇到了以下情况:

使用int字段的以下结构是完全合法的:

struct MyStruct
{ 
    public MyStruct ( int size ) 
    { 
        this.Size = size; // <-- Legal assignment.
    } 

    public int Size; 
}
Run Code Online (Sandbox Code Playgroud)

但是,使用自动属性的以下结构无法编译:

struct MyStruct
{ 
    public MyStruct ( int size ) 
    { 
        this.Size = size; // <-- Compile-Time Error!
    } 

    public int Size{get; set;}
}
Run Code Online (Sandbox Code Playgroud)

返回的错误是"在将所有字段分配给"之前,不能使用'this'对象.我知道这是结构的标准过程:任何属性的支持字段必须直接从结构的构造函数中指定(而不是通过属性的set访问器).

解决方案是使用显式支持字段:

struct MyStruct
{ 
    public MyStruct(int size)
    {
        _size = size;
    }

    private int _size;

    public int Size
    {
        get { return _size; }
        set { _size = value; …
Run Code Online (Sandbox Code Playgroud)

c# struct automatic-properties c#-3.0

64
推荐指数
3
解决办法
2万
查看次数

C#中的自动属性是什么?它们的用途是什么?

有人可以提供C#中自动属性的简单解释,它们的用途,也许还有一些例子吗?请尽量保持外行的条件!

.net c# syntax properties automatic-properties

59
推荐指数
4
解决办法
6万
查看次数

C#自定义getter/setter没有私有变量

我最近学到了c#,所以当我学会写属性时,我被教会这样做:

public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)

汽车房产很棒!但是现在我正在尝试做一些更复杂的事情,所以我需要编写一对自定义访问器.

private string _Name;
public string Name {
    get { return _Name; }
    set { _Name = value }
}
Run Code Online (Sandbox Code Playgroud)

我知道当一个人使用autos时,编译器会将私有实例变量放在它的黑暗深处,但是我被宠坏了并且不希望那个私有变量看起来毫无意义.

有没有办法在没有私有变量的情况下使用自定义访问器?

c# coding-style accessor automatic-properties

55
推荐指数
3
解决办法
6万
查看次数

为什么有必要在结构上调用:this()来使用c#中的自动属性?

如果我使用如下自动属性在C#中定义结构:

public struct Address
{
    public Address(string line1, string line2, string city, string state, string zip)
    {
        Line1 = line1;
        Line2 = line2;
        City = city;
        State = state;
        Zip = zip;
    }

    public string Line1 { get; protected set; }
    public string Line2 { get; protected set; }
    public string City { get; protected set; }
    public string State { get; protected set; }
    public string Zip { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试构建文件时,我收到编译错误说The 'this' object cannot be …

c# struct cil automatic-properties

53
推荐指数
1
解决办法
5535
查看次数