有什么样的.NET / C#或-等于从红宝石? [英] Is there anything like Or-Equals from Ruby in .NET/C#?

查看:149
本文介绍了有什么样的.NET / C#或-等于从红宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的东西在C#中,我做的所有的时间在Ruby中,我不知道是最接近会是什么。

如果枚举不包含我的整数值的定义,我希望它默认为一定值。我能做到这一条线?

红宝石十岁上下的分配(两个例子):

  namedStr = Enum.GetName(typeof运算(myEnum),enumedInt)|| 默认
 

  namedStr = Enum.GetName(typeof运算(myEnum),enumedInt)
namedStr || =默认
 

解决方案

  namedStr = Enum.GetName(typeof运算(myEnum),enumedInt)? 默认
 

I'm trying to do something in C# that I do all the time in Ruby, and I wondered what the closest thing would be.

If the Enum does not contain a definition for my integer value, I want it to default to a certain value. Can I do this in one line?

Ruby-ish assignment (two examples):

namedStr = Enum.GetName(typeof(myEnum), enumedInt) || "DEFAULT"

or

namedStr = Enum.GetName(typeof(myEnum), enumedInt)
namedStr ||= "DEFAULT"

解决方案

namedStr = Enum.GetName(typeof(myEnum), enumedInt) ?? "DEFAULT"

这篇关于有什么样的.NET / C#或-等于从红宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆