你怎么能用enumC#枚举一下?
例如,以下代码无法编译:
public enum Suit
{
Spades,
Hearts,
Clubs,
Diamonds
}
public void EnumerateAllSuitsDemoMethod()
{
foreach (Suit suit in Suit)
{
DoSomething(suit);
}
}
Run Code Online (Sandbox Code Playgroud)
并给出以下编译时错误:
'Suit'是'type',但用作'变量'
Suit关键字失败,第二个关键字失败.
我知道PHP没有本机枚举.但我已经从Java世界习惯了它们.我希望使用枚举作为一种方式来提供IDE的自动完成功能可以理解的预定义值.
常量可以解决问题,但是存在名称空间冲突问题,并且(或实际上因为)它们是全局的.数组没有命名空间问题,但是它们太模糊了,它们可以在运行时覆盖,IDE很少(从不?)知道如何自动填充其键.
您是否经常使用任何解决方案/解决方法?有谁回忆一下PHP家伙是否对枚举有任何想法或决定?
在C#(随意回答其他语言)循环中,break和continue之间的区别是什么,作为离开循环结构的手段,并进入下一次迭代?
例:
foreach (DataRow row in myTable.Rows)
{
if (someConditionEvalsToTrue)
{
break; //what's the difference between this and continue ?
//continue;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找标准习语来迭代NSArray.我的代码需要适合OS X 10.4+.
我发现在Objective-C中访问可变字典键和值有些困难.
假设我有这个:
NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init];
Run Code Online (Sandbox Code Playgroud)
我可以设置键和值.现在,我只想访问每个键和值,但我不知道键的数量.
在PHP中它非常简单,如下所示:
foreach ($xyz as $key => $value)
Run Code Online (Sandbox Code Playgroud)
如何在Objective-C中实现?
enumeration objective-c nsdictionary key-value nsmutabledictionary
是否有关于何时使用案例类(或案例对象)与在Scala中扩展Enumeration的最佳实践指南?
他们似乎提供了一些相同的好处.
显然有很多方法可以迭代集合.好奇,如果有任何差异,或为什么你使用一种方式而不是另一种方式.
第一种:
List<string> someList = <some way to init>
foreach(string s in someList) {
<process the string>
}
Run Code Online (Sandbox Code Playgroud)
另一种方式:
List<string> someList = <some way to init>
someList.ForEach(delegate(string s) {
<process the string>
});
Run Code Online (Sandbox Code Playgroud)
我认为,除了我上面使用的匿名代表之外,我还有一个你可以指定的可重用代理......
考虑:
using System;
public class Test
{
enum State : sbyte { OK = 0, BUG = -1 }
static void Main(string[] args)
{
var s = new State[1, 1];
s[0, 0] = State.BUG;
State a = s[0, 0];
Console.WriteLine(a == s[0, 0]); // False
}
}
Run Code Online (Sandbox Code Playgroud)
怎么解释这个?当在x86 JIT中运行时,它出现在Visual Studio 2015中的调试版本中.x64 JIT中的发布版本或运行按预期打印True.
要从命令行重现:
csc Test.cs /platform:x86 /debug
Run Code Online (Sandbox Code Playgroud)
(/debug:pdbonly,/debug:portable并且/debug:full还复制.)
我有一个枚举:
public enum MyColours
{
Red,
Green,
Blue,
Yellow,
Fuchsia,
Aqua,
Orange
}
Run Code Online (Sandbox Code Playgroud)
我有一个字符串:
string colour = "Red";
Run Code Online (Sandbox Code Playgroud)
我希望能够回归:
MyColours.Red
Run Code Online (Sandbox Code Playgroud)
从:
public MyColours GetColour(string colour)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
public MyColours GetColours(string colour)
{
string[] colours = Enum.GetNames(typeof(MyColours));
int[] values = Enum.GetValues(typeof(MyColours));
int i;
for(int i = 0; i < colours.Length; i++)
{
if(colour.Equals(colours[i], StringComparison.Ordinal)
break;
}
int value = values[i];
// I know all the information about the matched enumeration
// but how do i convert this information into returning a
// …Run Code Online (Sandbox Code Playgroud) enumeration ×10
c# ×5
loops ×3
.net ×2
objective-c ×2
case-class ×1
cocoa ×1
enums ×1
generics ×1
iteration ×1
key-value ×1
map ×1
nsarray ×1
nsdictionary ×1
php ×1
range ×1
ruby ×1
scala ×1
string ×1