有什么样的.NET / C#或-等于从红宝石?红宝石、NET

2023-09-06 08:25:00 作者:凉心凉情菇凉百毒不侵/凉情凉心姑娘早已绝情

我想要做的东西在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"