本文介绍了使用构造函数来处理“非平凡事物”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我今天正在读一本叫O'Reilly的编程C#5.0的书,正在阅读更深入的关于构造函数的内容,我找到了这个; 如果你编写一个构造函数来使用你的类,那么你很可能会惊讶于使用你的类的开发人员,例如将数据添加到数据库或通过 $发送消息。 b $ b network。 这是正确的吗? 我是一个新的C#程序员,但对此感到惊讶(除非我一直以完全错误的方式学习),并且对这个问题感兴趣。 当我在CodeProject上看到很多例子时,我发现很多代码都发生在构造函数中(这对我来说有意义吗?)。 感谢您抽出宝贵的时间! 看来我把构造函数与方法混淆了......非常愚蠢的错误。感谢你们所有人的回复时间。I was reading a book today called "Programming C# 5.0" by O'Reilly and was reading in more depth about constructors, I found this;"You are likely to surprise developers who use your class if you write a constructor that doessomething non-trivial, such as adding data to a database or sending a message over thenetwork."Would this be correct?I'm a newish C# programmer but was surprised by this (unless I have been learning in a completely wrong way) and was interested on the thought on this.When I look at many examples on CodeProject I find alot of code takes place in the constructors (which would make sense to me?).Thanks for your time!It appears I had confused a Constructor with a Method...very silly mistake. I thank all of you for your time for replying.推荐答案正如其他人已经注意到的那样,提示就在名称中。构造函数应该按照他们的名字建议:初始化对象,即将对象置于众所周知,可靠的状态,不能少,不再存在。少花钱就是灾难性的,做得更多就是坏事。As others have already noted, the hint is in the name. Constructors should do what their name suggest: initialize the object, that is put the object in well known, reliable state, no less, no more. Doing less is catastrophic, doing more is bad. 这篇关于使用构造函数来处理“非平凡事物”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-18 13:31