本文介绍了干将,setter方法​​和属性的最佳做法。 Java的与C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在一个C#类现在,我试图找出做事情的最好方法。我来自一个Java背景,所以我只熟悉Java的最佳做法;我是一个C#新手!

I'm taking a C# class right now and I'm trying to find out the best way of doing things. I come from a Java background and so I'm only familiar with Java best-practices; I'm a C# novice!

在Java的,如果我有一个私人财产,我这样做;

In Java if I have a private property, I do this;

private String name;

public void setName(String name) {
   this.name = name;
}

public String getName() {
   return this.name;
}

在C#中,我看到有这样做的许多方面。

In C#, I see that there are many ways of doing this.

我能做到像Java:

private string name;

public void setName(string name) {
   this.name = name;
}

public string getName() {
   return this.name;
}

或者,我可以做这种方式:

Or I can do it this way:

private string name;

public string Name {
   get { return name; }
   set { name = value; }
}

或者

public string Name { get; set; }

哪一个我应该使用,什么是涉及到每一种方法的告诫或微妙之处?当创建类,我下面一般的最佳实践,我从Java知道(尤其是阅读有效的Java)。因此,例如,我赞成不变性(提供setter方法​​只在必要时)。我只是好奇,想看看这些做法如何与提供在C#中的getter和setter的各种方法;本质上,我将如何转换从Java世界的最佳实践为C#?

Which one should I use, and what are the caveats or subtleties involved with each approach? When creating classes, I am following general best-practices that I know from Java (especially reading Effective Java). So for example, I am favoring immutability (providing setters only when necessary). I'm just curious to see how these practices fit in with the various ways of providing setters and getters in C#; essentially, how would I translate best-practices from the Java world into C#?

修改

我张贴这种以乔恩斯基特的回答评论,但随后长了:

I was posting this as a comment to Jon Skeet's answer but then it got long:

样一个非平凡属性(即,具有显著处理和验证也许)?可能我还是通过公共财产但封装在 GET 逻辑揭露它和设置?为什么会/要我做这在具有专用的setter和getter方法​​(关联处理和验证逻辑)。

What about a non-trivial property (i.e., with significant processing and validation perhaps)? Could I still expose it via a public property but with the logic encapsulated in get and set? Why would/should I do this over having dedicated setter and getter methods (with associated processing and validation logic).

推荐答案

pre-C#6

我用最后的这些,对于一个微不足道的属性。请注意,我称之为的公共的财产无论是getter和setter方法​​是公开的。

I'd use the last of these, for a trivial property. Note that I'd call this a public property as both the getters and setters are public.

不变性是有点与自动实现属性痛苦的 - 你不能写一个自动财产只有一个getter;你能来最接近的是:

Immutability is a bit of a pain with automatically implemented properties - you can't write an auto-property which only has a getter; the closest you can come is:

public string Foo { get; private set; }

这是不是的真正的不可变的...只是一成不变的类之外。所以,你可能希望使用的真正的只读属性,而不是:

which isn't really immutable... just immutable outside your class. So you may wish to use a real read-only property instead:

private readonly string foo;
public string Foo { get { return foo; } }

您肯定不希望写的getName()的setName()。在部分的情况下,它是有道理的写get / set方法,而不是使用属性,尤其是当它们可能是昂贵的,你要强调的是。但是,你要遵循PascalCase对方法.NET命名约定,你不会想要一个简单的财产像这样与正常无论如何方法来实现 - 一个属性是更地道此处

You definitely don't want to write getName() and setName(). In some cases it makes sense to write Get/Set methods rather than using properties, particularly if they could be expensive and you wish to emphasize that. However, you'd want to follow the .NET naming convention of PascalCase for methods, and you wouldn't want a trivial property like this to be implemented with normal methods anyway - a property is much more idiomatic here.

C#6

太棒了,我们终于等来了合适的只读自动实现的属性:

Hooray, we finally have proper read-only automatically implemented properties:

// This can only be assigned to within the constructor
public string Foo { get; }

有关其的只读属性同样做的需要做一些工作,就可以使用会员浓郁的属性:

Likewise for read-only properties which do need to do some work, you can use member-bodied properties:

public double Area => height * width;

这篇关于干将,setter方法​​和属性的最佳做法。 Java的与C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:42