一种在 IF 之间编写逻辑和传递值的简单方法
有没有更简单的方法来实现这一逻辑? 它经常被使用,但每次我都觉得它是不必要的。
if (x != y)
x = y;
在更耗时的方法中,您必须使用变量,以免每次都重新计算它们。
我认为必须有一种更好的方法,例如:
var MyF = CheckSomething();
if (X != MyF)
X = MyF;
我有一个类名 Person
。它有一个属性信息
。
如果更改此字段的值,系统将根据设置的值执行计算,从而更改系统中其他字段的内容。
问题是,当该字段的内容与我必须比较的值相同时,我不想更改该字段的内容。
因为它会显着减慢程序速度。
我想知道是否有比每次为我要检查的每个字段编写上述条件更简单的方法。
我无法更改此类,我所能做的就是设置属性值。
问题是有数百个此类具有数十个属性
(主题)类似的问题和解决方案:
string SomeValue = SetValue();
string NewValue;
if (SomeValue == null)
NewValue = "NULL RECEIVED";
else
NewValue = SomeValue;
与相同
string SomeValue = SetValue();
string NewValue = SomeValue ?? "NULL RECEIVED";
并且这个快捷方式就是我正在寻找的。
Is there an easier way to do this logical?
It is used often and each time I have the impression that it is unnecessary.
if (x != y)
x = y;
In more time-consuming methods, you have to use variables so as not to recalculate them every time.
I think there has to be a better way than, for example:
var MyF = CheckSomething();
if (X != MyF)
X = MyF;
I have a class name Person
. It has a property Information
.
If you change the value of this field, the system performs calculations which, based on the value set, changes the contents of other fields in the system.
The problem is that I don't want to change the contents of this field when it is identical to the value I have to compare.
Because it will slow down the program significantly.
And I wonder if there is an easier way than writing the above condition each time for each field that I want to check.
I can't change this class, all I can do is set the property value.
The problem is that there are hundreds of these classes with dozens of properties
(Thematic) Similar problem and solution :
string SomeValue = SetValue();
string NewValue;
if (SomeValue == null)
NewValue = "NULL RECEIVED";
else
NewValue = SomeValue;
Is the same as
string SomeValue = SetValue();
string NewValue = SomeValue ?? "NULL RECEIVED";
And this shortcut is what I'm looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议是使用 自包含块,以减少携带值的变量到其关联的
if
语句:另一个想法可能是使用通用的
EnsureValue
方法:这是
EnsureValue
的实现:我不认为它会让你的代码更具可读性。
My suggestion is to use self containing blocks, in order to reduce the scope of the value-carrying variables to their associated
if
statements:Another idea could be to use a generic
EnsureValue
method:Here is the implementation of the
EnsureValue
:I don't think that it makes your code more readable though.