Getter、setter 和属性最佳实践.Java 与 C# [英] Getters, setters, and properties best practices. Java vs. C#

查看:17
本文介绍了Getter、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 那样做:

I can do it like Java:

private string name;

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

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

或者我可以这样做:

private string name;

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

或者:

public string Name { get; set; }

我应该使用哪种方法,每种方法有哪些注意事项或微妙之处?创建类时,我遵循从 Java 中了解到的一般最佳实践(尤其是阅读 Effective Java).例如,我支持不变性(仅在必要时提供 setter).我只是想知道这些实践如何与 C# 中提供 setter 和 getter 的各种方式相适应;本质上,我如何将 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#?

编辑

我将其发布为对 Jon Skeet 的回答的评论,但后来很长:

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

一个非平凡的财产(即可能有重要的处理和验证)怎么样?我是否仍然可以通过公共属性公开它,但将逻辑封装在 getset 中?为什么我会/应该这样做而不是拥有专用的 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

我会使用最后一个,作为一个微不足道的属性.请注意,我将其称为 public 属性,因为 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; }

这不是真的不可变的......只是在你的课堂之外是不可变的.因此,您可能希望改用 real 只读属性:

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;

这篇关于Getter、setter 和属性最佳实践.Java 与 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆