一种在 IF 之间编写逻辑和传递值的简单方法

发布于 2025-01-10 15:50:22 字数 813 浏览 0 评论 0原文

有没有更简单的方法来实现这一逻辑? 它经常被使用,但每次我都觉得它是不必要的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浴红衣 2025-01-17 15:50:22

我的建议是使用 自包含块,以减少携带值的变量到其关联的 if 语句:

{ var v = GetInformation(); if (person.Information != v) person.Information = v; }
{ var v = GetAddress(); if (person.Address != v) person.Address = v; }
{ var v = GetPhone(); if (person.Phone != v) person.Phone = v; }

另一个想法可能是使用通用的 EnsureValue 方法:

EnsureValue(person, x => x.Information, (x, v) => x.Information = v, GetInformation());
EnsureValue(person, x => x.Address, (x, v) => x.Address = v, GetAddress());
EnsureValue(person, x => x.Phone, (x, v) => x.Phone = v, GetPhone());

这是 EnsureValue 的实现:

public static void EnsureValue<TSource, TProperty>(TSource source,
    Func<TSource, TProperty> getter,
    Action<TSource, TProperty> setter,
    TProperty value,
    IEqualityComparer<TProperty> comparer = default)
{
    comparer ??= EqualityComparer<TProperty>.Default;
    if (comparer.Equals(getter(source), value)) return;
    setter(source, value);
}

我不认为它会让你的代码更具可读性。

My suggestion is to use self containing blocks, in order to reduce the scope of the value-carrying variables to their associated if statements:

{ var v = GetInformation(); if (person.Information != v) person.Information = v; }
{ var v = GetAddress(); if (person.Address != v) person.Address = v; }
{ var v = GetPhone(); if (person.Phone != v) person.Phone = v; }

Another idea could be to use a generic EnsureValue method:

EnsureValue(person, x => x.Information, (x, v) => x.Information = v, GetInformation());
EnsureValue(person, x => x.Address, (x, v) => x.Address = v, GetAddress());
EnsureValue(person, x => x.Phone, (x, v) => x.Phone = v, GetPhone());

Here is the implementation of the EnsureValue:

public static void EnsureValue<TSource, TProperty>(TSource source,
    Func<TSource, TProperty> getter,
    Action<TSource, TProperty> setter,
    TProperty value,
    IEqualityComparer<TProperty> comparer = default)
{
    comparer ??= EqualityComparer<TProperty>.Default;
    if (comparer.Equals(getter(source), value)) return;
    setter(source, value);
}

I don't think that it makes your code more readable though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文