我今天在代码库中遇到了一些单例代码,我不确定以下是否是线程安全的:
public static IContentStructure Sentence{
get {
return _sentence ?? (_sentence = new Sentence());
}
}
Run Code Online (Sandbox Code Playgroud)
该陈述相当于:
if (_sentence != null) {
return _sentence;
}
else {
return (_sentence = new Sentence());
}
Run Code Online (Sandbox Code Playgroud)
我相信 ??只是一个编译器技巧,结果代码仍然不是原子的.换句话说,在将_sentence设置为新句子并返回之前,两个或多个线程可以找到_sentence为null.
为了保证原子性,我们必须锁定那段代码:
public static IContentStructure Sentence{
get {
lock (_sentence) { return _sentence ?? (_sentence = new Sentence()); }
}
}
Run Code Online (Sandbox Code Playgroud)
那一切都正确吗?
当我使用LINQ将XML文件解析为自定义对象时,我试图阻止具有NULL值.
我在Scott Gu的博客上找到了一个很好的解决方案,但由于某些原因它对我的整数不起作用.我想我使用了相同的语法,但似乎我错过了一些东西.哦,由于某种原因,当节点不为空时它可以工作.
下面是我的代码的摘录.
List<GrantAgresso> lsResult = (from g in xml.Element("root").Elements("Elementname")
select new GrantAgresso()
{
Year = (int?)g.Element("yearnode") ?? 0,
Subdomain = (string)g.Element("domainnode") ?? ""
}).ToList();
Run Code Online (Sandbox Code Playgroud)
错误消息是:
输入字符串的格式不正确.
如果有人知道我做错了什么,请帮忙:)
编辑:一块XML(奇怪的名字,但它不是选择)
<Agresso>
<AgressoQE>
<r3dim_value>2012</r3dim_value>
<r0r0r0dim_value>L5</r0r0r0dim_value>
<r7_x0023_province_x0023_69_x0023_V005>0</r7_x0023_province_x0023_69_x0023_V005>
<r7_x0023_postal_code_x0023_68_x0023_V004 />
<r7_x0023_country_x0023_67_x0023_V003>1004</r7_x0023_country_x0023_67_x0023_V003>
<r7_x0023_communitydistrict_x0023_70_x0023_V006>0</r7_x0023_communitydistrict_x0023_70_x0023_V006>
</AgressoQE>
</Agresso>
Run Code Online (Sandbox Code Playgroud) Python中是否有一个空传播运算符(“空感知成员访问”运算符),所以我可以写一些类似的东西
var = object?.children?.grandchildren?.property
Run Code Online (Sandbox Code Playgroud)
就像在 C#、VB.NET 和 TypeScript 中一样,而不是
var = None if not myobject\
or not myobject.children\
or not myobject.children.grandchildren\
else myobject.children.grandchildren.property
Run Code Online (Sandbox Code Playgroud) python null-coalescing-operator null-coalescing method-chaining
通过以下对C#中的空合并运算符(??)的理解.
int? input = -10;
int result = input ?? 10;//Case - I
//is same as:
int result = input == null? input : 10; // Case - II
Run Code Online (Sandbox Code Playgroud)
而根据定义和用法,案例I和案例II是相同的.
令人惊讶的是,在Case-I编译器中能够隐式转换int吗?to case而在Case-II中它显示错误:'错误1无法隐式转换类型'int?' 'int'"
关于null-coalescing运算符,我错过了什么?
谢谢你的关注.
可能重复:
javascript的null合并运算符?
在C#中,您可以这样做:
var obj = newObject ?? defaultObject;
Run Code Online (Sandbox Code Playgroud)
这表示,分配newObject到obj如果没有别的空分配defaultObject.我如何用javascript写这个?
我在某处发现了C#null coalescing operator'??'的实现:
implicit def coalescingOperator[T](pred: T) = new {
def ??[A >: T](alt: =>A) = if (pred == null) alt else pred
}
Run Code Online (Sandbox Code Playgroud)
然后可以像使用a ?? b哪种方式一样使用它if (a == null) b else a.
在反编译类文件后,我看到它生成带反射的代码(在Scala 2.8.1中).
为什么它会生成反射,是否可以修改该代码,以免产生反射?
我们用?? 运算符根据空值计算表达式,例如:
string foo = null;
string bar = "woooo";
string foobar= foo ?? bar ;
// Evaluates foobar as woooo
Run Code Online (Sandbox Code Playgroud)
我们还使用了一个if语句,如果与上面的表达式一起使用,它的作用相同
string foo = null;
string bar = "woooo";
if(foo==null)
string foobar= "woooo" ;
// Evaluates foobar as woooo same as above
Run Code Online (Sandbox Code Playgroud)
还有?:三元运营商 ......
string foo = null;
string bar = "woooo";
string foobar= foo==null ? "woooo" : null ;
// Evaluates foobar as woooo same as above
Run Code Online (Sandbox Code Playgroud)
我知道空合并在语法上是精确的,但哪一个在两者之间编译得更快,执行速度更快,为什么?
我想要一个简洁安全的干净/紧凑模式rulesKey,即使抛出某些东西也会妥善处理.这似乎是不可能的using(除非可能4 using秒,但看起来如此冗长并打开额外的资源,我甚至可能不需要打开).让我感到惊讶的是,这在C++中会如此直接/轻松.有一个很好的解决方案吗?
{
RegistryKey rulesKey = null;
rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Company\\Internal\\Product");
rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Company\\Company\\Internal\\Product");
rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\Company\\Product");
rulesKey = rulesKey ?? Registry.LocalMachine.OpenSubKey("Software\\Company\\Product");
// Code using rulesKey, might throw
rulesKey.Close();
}
Run Code Online (Sandbox Code Playgroud) 我目前在Unity中设置了两个脚本来处理一些UI音频.一个是经理,另一个是为特定的UI元素播放声音.我所拥有的简化版本如下:
public class AudioUIManager : MonoBehaviour //Only one of these in the scene
{
public AudioClip genericUISound; //This is set in the inspector.
}
Run Code Online (Sandbox Code Playgroud)
public class AudioUITextAnimation : MonoBehaviour
{
[SerializeField]
private AudioClip specifiedUISound; //This is not set in the inspector
[SerializeField]
private AudioUIManager audioUIManager; // I get a reference to this elsewhere
void Start()
{
//Use generic sounds from audio manager if nothing is specified.
specifiedUISound = specifiedUISound ?? audioUIManager.genericUISound;
print(specifiedUISound);
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里想要实现的是让该specifiedUISound字段使用在检查器中分配给它的声音.如果未分配声音,则使用UI管理器中的通用声音.这样可以节省我为需要相同声音的数百万个按钮分配相同的声音,但如果我愿意,可以选择为一个按钮设置特定的声音.
然而,在空coalessing算asigning null …
c# null serialization null-coalescing-operator unity-game-engine
我有三个继承自'IFoo'接口的类(classA,classB和classC); 如果使用这个
var fooItem = (request.classAitem ?? (request.classBitem as IFoo ?? request.classCitem))
Run Code Online (Sandbox Code Playgroud)
要么
var fooItem = (request.classAitem ?? request.classBitem ?? request.classCitem as IFoo)
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但其他组合甚至不会编译:
var fooItem = (request.classAitem as IFoo ?? request.classBitem ?? request.classCitem)
Run Code Online (Sandbox Code Playgroud)
要么
var fooItem = (request.classAitem ?? request.classBitem ?? request.classCitem) as IFoo
Run Code Online (Sandbox Code Playgroud)
在我看来,在某些情况下,编译器隐式地将子类解包到它们的IFoo接口,但在其他一些情况下却没有.你们有什么感想?