相关疑难解决方法(0)

如何为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万
查看次数

是否可以在声明点初始化属性

想象一下,你在课堂上有一个字段_items.您可以在声明点初始化它:

class C
{
  IList<string> _items=new List<string>();
}
Run Code Online (Sandbox Code Playgroud)

现在我想将此字段转换为自动生成的属性,但现在初始化无效:

class C
{
  public IList<string> Items=new List<string>(); {get; set;} // Invalid
}
Run Code Online (Sandbox Code Playgroud)

所以,我必须这样做:

class C
{
  public IList<string> Items {get; set;}

  public C
  {
    Items=new List<string>();
  }
}
Run Code Online (Sandbox Code Playgroud)

但这并不像在声明点初始化字段那样方便.有没有更好的方法来执行此操作,而不必(不必要地)使用私有(在声明点初始化)字段来支持此属性,例如.

谢谢

c# c#-4.0

16
推荐指数
2
解决办法
9755
查看次数

为什么C#自动属性不支持VB 2010等默认值?

看看新的VB 2010功能,我偶然发现了对Auto-Implemented Properties的支持.

由于我正在使用C#,这看起来很熟悉,但我注意到VB确实添加了我希望在C#中拥有的功能:为自动实现的属性设置任意默认值:

Public Class Person

    Property Name As String = "Scott Guthrie"
    Property Age as Integer = 35

End Class
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢在C#中使用自动属性.这样可以节省我们每次只需要默认值时引入支持字段并将其挂接到属性的工作量,从而不必要地使代码混乱.

我想知道为什么这也不是在C#中引入的?不这样做的理由是什么?是否正在进行语法讨论,或者实现此方法是否存在技术限制?

c# vb.net .net-4.0 automatic-properties

8
推荐指数
2
解决办法
1459
查看次数

设置自动实现属性的默认值

假设我有一个自动实现的属性

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

无论如何都要将默认值设置SheetNum为1,所以它会是这样的

private int sheetNum = 1;

public int SheetNum
{
    set { this.sheetNum = value; }
    get { return this.sheetNum; }
}
Run Code Online (Sandbox Code Playgroud)

c# getter-setter

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

你能用短手定义一个'新'属性吗?

我最近已经将一些代码从VB转换为C#,我注意到在VB中你可以用速记启动一个新的obj,这在C#中是可能的,还是你必须使用支持字段.

Public Property MyList As New List(Of String)
Run Code Online (Sandbox Code Playgroud)

似乎C#等价物是:

private List<String> _myList = new List<string>();
public List<String> MyList
{
    get { return _myList; }
    set { _myList = value; }
}
Run Code Online (Sandbox Code Playgroud)

注意*使用快捷命令'propfull'可以更轻松地写出这个的痛苦

c# vb.net

4
推荐指数
2
解决办法
266
查看次数

使属性getter和setter等于某事(auto-property initializer)

有什么区别:

public List<MyType> Something{ get; set; } = new List<MyType>();
Run Code Online (Sandbox Code Playgroud)

public List<MyType> Something{ 
    get{
        return new List<MyType>();
    }
    //set...
}
Run Code Online (Sandbox Code Playgroud)

上下文:
我不确定我在代码中看到的行为.构造函数null上有一个服务,但在未来的方法调用中,我假设的是类的相同实例.

c# properties

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

如何在C#Automatic Properties中返回对象的新实例

是否可以使用C#Automatic Properties创建对象的新实例?

在C#我喜欢我能做到这一点:

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

是否可以为首先需要实例化的List之类的对象执行此操作?

即:

List<string> LongProp  = new List<string>(); 
public List<string> LongProp {  
    get {  
        return LongProp ;  
    }  
    set {  
         LongProp  = value;  
    }  
}
Run Code Online (Sandbox Code Playgroud)

.net c# properties automatic-properties

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

C#访问器和初始化

我们如何组合c#访问器声明和初始化

List<string> listofcountries= new List<string>();
and 
List<string>listofcountries {get;set;}
Run Code Online (Sandbox Code Playgroud)

有没有办法将这些与声明结合起来?

.net c#

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

代码比较 - 哪个更好或更不必要?

可能重复:
C#中属性和字段之间的差异

public class Test
{
    public bool testData1;
    public string testData2;
}
Run Code Online (Sandbox Code Playgroud)

要么

public class Test
{
    public bool TestData1 { get; set; }
    public string TestData2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

要么

public class Test
{
    private bool testData1;
    private string testData2;

    public bool TestData1 { get { return testData1; } set { testData1 = value; } }
    public string TestData2 { get { return testData2; } set { testData2 = value; } }
}
Run Code Online (Sandbox Code Playgroud)

哪种优化代码更好或更不必要?为什么?

这不是最后一个持有很多不必要的数据吗?

=======编辑: …

c#

0
推荐指数
2
解决办法
162
查看次数