In my last c# interview, I was asked to prove immutability of C# string,I know what is meant by immutability of c# string,But is it possible to prove immutability of c# string through code ? can i have a sample code snippet please. Thanks in advance I can prove that a string is not immutable. All I need to do is to show some code which mutates a string , like so: using System; using Syste
在我上一次的c#访谈中, 我被要求证明C#字符串的不变性,我知道c#字符串的不变性是什么意思,但是有可能通过代码证明c#字符串的不变性吗? 我可以有一个示例代码片段吗? 提前致谢 我可以证明一个string不是不可变的。 我需要做的就是显示一些代码,这些代码会改变string ,如下所示: using System; using System.Runtime.InteropServices; namespace Demo { class Program { static void Main(str
I'd like to create a generic method for converting any System.Enum derived type to its corresponding integer value, without casting and preferably without parsing a string. Eg, what I want is something like this: // Trivial example, not actually what I'm doing. class Converter { int ToInteger(System.Enum anEnum) { (int)anEnum; } } But this doesn't appear to work.
我想创建一个通用的方法,用于将任何System.Enum派生类型转换为相应的整型值,而无需强制转换并且最好不分析字符串。 例如,我想要的是这样的: // Trivial example, not actually what I'm doing. class Converter { int ToInteger(System.Enum anEnum) { (int)anEnum; } } 但是这似乎并不奏效。 Resharper报告说您不能将类型'System.Enum'的表达式转换为键入'int'。 现在我已经想
This question already has an answer here: Get int value from enum in C# 24 answers To get all values of the Enum try this var allValues = Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); and your totalSteps property should look like this public int[] totalSteps { get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); } } how can I return it in "
这个问题在这里已经有了答案: 从C#中的枚举中获得int值的24个答案 要获取Enum的所有值,请尝试此操作 var allValues = Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); 你的totalSteps属性应该是这样的 public int[] totalSteps { get { return Enum.GetValues(typeof(TotalSteps)).Cast<int>().ToArray(); } } 我怎样才能在“get”函数中返回它? 该值无法转换 属性(我猜)被定义为获取
This question already has an answer here: Get int value from enum in C# 24 answers Just cast the enum value to int . A a = new A(); int k = (int)a.NumberOfFiscalPeriod; The inverse is valid too, even if there is no enum value defined for that int value, for sample: int i = 13; // int value EFiscalPeriond f = (EFiscalPeriond) i; // converting int to enum
这个问题在这里已经有了答案: 从C#中的枚举中获得int值的24个答案 只需将枚举值转换为int 。 A a = new A(); int k = (int)a.NumberOfFiscalPeriod; 即使没有为该int值定义的枚举值,反例也是有效的,对于示例: int i = 13; // int value EFiscalPeriond f = (EFiscalPeriond) i; // converting int to enum
This question already has an answer here: Get int value from enum in C# 24 answers 你只需要(int)logLevel - 也就是说,你将logLevel转换为一个int。
这个问题在这里已经有了答案: 从C#中的枚举中获得int值的24个答案 你只需要(int)logLevel - 也就是说,你将logLevel转换为一个int。
Possible Duplicate: Enums returning int value I want to numerate "Stack" , "Overflow" and "Stackoverflow" . Therefore I use enumerate. enum Status : int { Stack = 1, Overflow = 2, StackOverflow = 3 } But when I want to get value of Stack, I get "Stack" . int status = 0; status = Status.Stack; //I want to assign 1 to status but Status.
可能重复: 枚举返回int值 我想要说明"Stack" , "Overflow"和"Stackoverflow" 。 因此我使用枚举。 enum Status : int { Stack = 1, Overflow = 2, StackOverflow = 3 } 但是当我想要获得Stack的价值时,我会得到"Stack" 。 int status = 0; status = Status.Stack; //I want to assign 1 to status but Status.Stack returns "Stack" 我该怎么做? 只需转换
Possible Duplicate: Enums returning int value How to get the numeric value from the Enum? THIS IS NOT A DUPLICATE OF EITHER OF THESE, THANKS FOR READING THE QUESTION MODS Suppose I have a number of my flags enum items selected: [Flags] public enum Options { None = 0, Option_A = 1, Option_B = 2, Option_C = 4, Option_D = 8, } Options selected = Options.Option_A | Optio
可能重复: 枚举返回int值 如何从Enum中获取数值? 这不是这些的重复,感谢读取问题MODS 假设我有一些标志枚举项被选中: [Flags] public enum Options { None = 0, Option_A = 1, Option_B = 2, Option_C = 4, Option_D = 8, } Options selected = Options.Option_A | Options.Option_B; selected的值应该对应于3(即2 + 1) 我怎样才能把它变成一个int? 我看过一些例子,其中selected是To
Possible Duplicate: How do I Convert a string to an enum in C#? Enums returning int value I have declare an enumeration:- public enum Car { SELECT = 0, AUDI = 1, NISSAN = 2, HONDA = 3, LINCOLN = 4 } Now I need the int value of enum where it matches:- private int GetCarType(string CarName) { f
可能重复: 如何将字符串转换为C#中的枚举? 枚举返回int值 我已经声明了一个枚举: - public enum Car { SELECT = 0, AUDI = 1, NISSAN = 2, HONDA = 3, LINCOLN = 4 } 现在我需要enum的int值,它匹配: - private int GetCarType(string CarName) { foreach(var item in Enum.GetNames(typeof(Car))
This question already has an answer here: Get int value from enum in C# 24 answers In addition to @dtb You can specify the int (or flag) of your enum by supplying it after the equals sign. enum MyEnum { Foo = 0, Bar = 100, Baz = 9999 } Cheers If you have an enum such as enum MyEnum { Foo, Bar, Baz, } and a value of that enum such as MyEnum value = MyEnum.Foo;
这个问题在这里已经有了答案: 从C#中的枚举中获得int值的24个答案 除了@dtb 您可以通过在等号后面提供枚举来指定枚举的int(或标志)。 enum MyEnum { Foo = 0, Bar = 100, Baz = 9999 } 干杯 如果你有一个枚举类型如 enum MyEnum { Foo, Bar, Baz, } 以及该枚举的值,如 MyEnum value = MyEnum.Foo; 那么将该值转换为int的最佳方式是 int result = (int)value; 我会在Enum上以扩展方法
This question already has an answer here: Get int value from enum in C# 24 answers To answer your question "how can I get the integer for string value": To convert from a string into an enum, and then convert the resulting enum to an int, you can do this: public enum Animals { dog = 0, cat = 1, rat = 2 } ... Animals answer; if (Enum.TryParse("CAT", true, out answer
这个问题在这里已经有了答案: 从C#中的枚举中获得int值的24个答案 要回答你的问题“我怎样才能得到字符串值的整数”: 要将字符串转换为枚举,然后将生成的枚举转换为int,可以这样做: public enum Animals { dog = 0, cat = 1, rat = 2 } ... Animals answer; if (Enum.TryParse("CAT", true, out answer)) { int value = (int) answer; Console.WriteLine(value); } 顺便说一句,正常的枚举命