C# 反模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1529604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Antipatterns
提问by exhuma
To cut a long story short: I find the Java antipatternsan indispensable resource. For beginners as much as for professionals. I have yet to find something like this for C#. So I'll open up this question as community wiki and invite everyone to share their knowledge on this. As I am new to C#, I am strongly interested in this, but cannot start with some antipatterns :/
长话短说:我发现Java 反模式是不可或缺的资源。对于初学者和专业人士一样。我还没有为 C# 找到类似的东西。所以我将这个问题作为社区维基公开,并邀请每个人分享他们的知识。由于我是 C# 的新手,我对此非常感兴趣,但不能从一些反模式开始:/
Here are the answers which I find specifically true for C# and not other languages.
以下是我发现特别适用于 C# 而不是其他语言的答案。
I just copy/pasted these! Consider throwing a look on the comments on these as well.
我只是复制/粘贴这些!考虑看看对这些的评论。
Throwing NullReferenceException
投掷 NullReferenceException
Throwing the wrong exception:
抛出错误的异常:
if (FooLicenceKeyHolder == null)
throw new NullReferenceException();
Properties vs. public Variables
属性与公共变量
Public variables in classes (use a property instead).
类中的公共变量(改用属性)。
Unlessthe class is a simple Data Transfer Object.
除非该类是一个简单的数据传输对象。
Not understanding that bool is a real type, not just a convention
不明白 bool 是一个真正的类型,而不仅仅是一个约定
if (myBooleanVariable == true)
{
...
}
or, even better
或者,甚至更好
if (myBooleanVariable != false)
{
...
}
Constructs like these are often used by C
and C++
developers where the idea of a boolean value was just a convention (0 == false, anything else is true); this is not necessary (or desirable) in C# or other languages that have real booleans.
这样的结构通常使用C
和C++
开发,其中一个布尔值的想法只是一个约定(0 ==假的,什么都为true); 这在 C# 或其他具有真正布尔值的语言中不是必需的(或不可取的)。
Using using()
使用 using()
Not making use of using
where appropriate:
没有using
在适当的地方使用:
object variable;
variable.close(); //Old code, use IDisposable if available.
variable.Dispose(); //Same as close. Avoid if possible use the using() { } pattern.
variable = null; //1. in release optimised away. 2. C# is GC so this doesn't do what was intended anyway.
回答by Robert Harvey
Public variables in classes (use a property instead).
类中的公共变量(改用属性)。
Unlessthe class is a simple Data Transfer Object.
除非该类是一个简单的数据传输对象。
See comments below for discussion and clarification.
请参阅下面的评论以进行讨论和澄清。
回答by leppie
int foo = 100;
int bar = int.Parse(foo.ToString());
Or the more general case:
或者更一般的情况:
object foo = 100;
int bar = int.Parse(foo.ToString());
回答by Andrew Keith
is this considered general ?
这被认为是通用的吗?
public static main(string [] args)
{
quit = false;
do
{
try
{
// application runs here ..
quit = true;
}catch { }
}while(quit == false);
}
I dont know how to explain it, but its like someone catching an exception and retrying the code over and over hoping it works later. Like if a IOException occurs, they just try over and over until it works..
我不知道如何解释它,但这就像有人捕获异常并一遍又一遍地重试代码,希望它以后能正常工作。就像发生 IOException 一样,他们只是一遍又一遍地尝试,直到它起作用为止。
回答by Ralph Lavelle
Massively over-complicated 'Page_Load' methods, which want to do everything.
大量过于复杂的“Page_Load”方法,它们想要做任何事情。
回答by Spence
object variable;
variable.close(); //Old code, use IDisposable if available.
variable.Dispose(); //Same as close. Avoid if possible use the using() { } pattern.
variable = null; //1. in release optimised away. 2. C# is GC so this doesn't do what was intended anyway.
回答by Andrew Keith
GC.Collect()
to collect instead of trusting the garbage collector.
GC.Collect()
收集而不是信任垃圾收集器。
回答by leppie
Insulting the law of Demeter:
侮辱德墨忒尔定律:
a.PropertyA.PropertyC.PropertyB.PropertyE.PropertyA =
b.PropertyC.PropertyE.PropertyA;
回答by rein
Rethrowing the exception incorrectly. To rethrow an exception :
错误地重新抛出异常。重新抛出异常:
try
{
// do some stuff here
}
catch (Exception ex)
{
throw ex; // INCORRECT
throw; // CORRECT
throw new Exception("There was an error"); // INCORRECT
throw new Exception("There was an error", ex); // CORRECT
}
回答by leppie
Private auto-implemented properties:
私有自动实现的属性:
private Boolean MenuExtended { get; set; }
回答by anthony
- Lack of null test before delegate invocation.
- Not knowing when and how to use 'as' with a null check vs. a cast with a try/catch.
- 'throw exception' vs. 'throw' within a catch block.
- Instantiating a large number of strings instead of using StringBuilder.
- Deep nesting of using blocks.
- 委托调用前缺少空测试。
- 不知道何时以及如何使用带有空检查的“as”与带有 try/catch 的强制转换。
- 在 catch 块中“抛出异常”与“抛出”。
- 实例化大量字符串而不是使用 StringBuilder。
- 使用块的深层嵌套。