如果您对Delphi–相当于C#的三元运算符?和delphi运算符用法感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Delphi–相当于C#的三元运算符?的各种细节,并对delphi运算符用法
如果您对Delphi – 相当于C#的三元运算符?和delphi 运算符用法感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Delphi – 相当于C#的三元运算符?的各种细节,并对delphi 运算符用法进行深入的分析,此外还有关于Bash 中的三元运算符 (?:)、c# – 嵌套的三元运算符、Delphi – 有没有相当于C#锁?、EL 表达式中的三元运算符的实用技巧。
本文目录一览:Delphi – 相当于C#的三元运算符?(delphi 运算符用法)
Possible Duplicate:
07000
我知道Delphi没有C#中的三进制运算符。
即:
那么如何最好地代表这个函数调用?那里最干净的方法是什么?
如果有任何代码可以使用INSTEAD写一个单独的函数会很好吗?如果没有,最有效和/或最干净的代码代码是什么?
解决方法
IfThen(SomeBooleanExpression,IfTrueReturnValue,IfFalseReturnValue)
返回值为数值(使用Math)或字符串(使用StrUtils)。但是请注意,这将在所有情况下评估两个参数 – 没有懒惰的评估,所以它不如C#中的?:运算符那么有效,只有正确的操作数被评估。
所以你不能做
y := IfThen(x <> 0,1/x,0)
最好的事情是坚持一个普通的
if x <> 0 then y := 1/x else y := 0;
Bash 中的三元运算符 (?:)
有没有办法做这样的事情
int a = (b == 5) ? c : d;
使用重击?
答案1
小编典典三元运算符? :
只是if/else
case "$b" in 5) a=$c ;; *) a=$d ;;esac
要么
[[ $b = 5 ]] && a="$c" || a="$d"
c# – 嵌套的三元运算符
_viewmodel.PhoneDefault = user == null ? "" : (string.IsNullOrEmpty(user.PhoneDay) ? (string.IsNullOrEmpty(user.PhoneEvening) ? (string.IsNullOrEmpty(user.Mobile) ? "" : user.Mobile) : user.PhoneEvening) : user.PhoneDay);
是否有更好的方法来编写它以使其更具可读性?
解决方法
// return the first parameter that isn't null or empty public static string CoalesceStrings(params string[] src) { foreach (var str in src) if (!string.IsNullOrEmpty(str)) return str; return ""; }
然后就这样打电话,就像这样:
_viewmodel.PhoneDefault = user == null ? "" : CoalesceStrings(user.PhoneDay,user.PhoneEvening,user.Mobile);
Delphi – 有没有相当于C#锁?
在C#中,我使用“lock”关键字:
private someMethod() { lock(mySharedobj) { //...do something with mySharedobj } }
在Delphi中,我找不到任何类似的东西,我发现只有TThread.Synchronize(someMethod)方法,它通过在主VCL线程中调用someMethod可以防止潜在的冲突,但这并不完全是我想要做的.
编辑:我使用的是Delphi 6
解决方法
TCriticalSection(注意:文档来自FreePascal,但它也存在于Delphi中):
示例代码:
type TSomeClass = class private FLock : TCriticalSection; public constructor Create(); destructor Destroy; override; procedure SomeMethod; end; constructor TSomeClass.Create; begin FLock := TCriticalSection.Create; end; destructor TSomeClass.Destroy; begin FreeAndNil(FLock); end; procedure TSomeClass.someMethod; begin FLock.Acquire; try //...do something with mySharedobj finally FLock.Release; end; end;
EL 表达式中的三元运算符
${emp.gender == 0 ? "male" : "female"}
今天的关于Delphi – 相当于C#的三元运算符?和delphi 运算符用法的分享已经结束,谢谢您的关注,如果想了解更多关于Bash 中的三元运算符 (?:)、c# – 嵌套的三元运算符、Delphi – 有没有相当于C#锁?、EL 表达式中的三元运算符的相关知识,请在本站进行查询。
本文标签: