Cast int to enum in C#

如何将int为C#中的enum ? From a string: YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString); // the foo.ToString().Contains(",") check is necessary for enumerations marked with an [Flags] attribute if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(",")) throw new InvalidOperationException($"{yourString} is not an underlying value of the YourEnum enu

在int中枚举枚举在C#中枚举

如何将int为C#中的enum ? 来自字符串: YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString); // the foo.ToString().Contains(",") check is necessary for enumerations marked with an [Flags] attribute if (!Enum.IsDefined(typeof(YourEnum), foo) && !foo.ToString().Contains(",")) throw new InvalidOperationException($"{yourString} is not an underlying value of the YourEnum enume

Token Based Authentication in ASP.NET Core

I'm working with ASP.NET Core application. I'm trying to implement Token Based Authentication but can not figure out how to use new Security System for my case. I went through examples but they didn't help me much, they are using either cookie authentication or external authentication (GitHub, Microsoft, Twitter). What my scenario is: angularjs application should request /token ur

ASP.NET Core中基于令牌的身份验证

我正在使用ASP.NET Core应用程序。 我试图实现基于令牌的身份验证,但无法弄清楚如何为我的情况使用新的安全系统。 我经历了一些例子,但他们并没有多大帮助,他们使用cookie身份验证或外部身份验证(GitHub,Microsoft,Twitter)。 我的场景是什么:angularjs应用程序应该请求/token url传递用户名和密码。 WebApi应授权用户并返回将在以下请求中由angularjs应用程序使用的access_token 。 我发现了一篇关于在当前版本

What are the correct version numbers for C#?

What are the correct version numbers for C#? What came out when? Why can't I find any answers about C# 3.5 ? This question is primarily to aid those who are searching for an answer using an incorrect version number, eg C# 3.5 . The hope is that anyone failing to find an answer with the wrong version number will find this question and then search again with the right version number. The

什么是C#的正确版本号?

什么是C#的正确版本号? 什么时候出来? 为什么我找不到关于C#3.5的任何答案? 这个问题主要是为了帮助那些正在使用不正确的版本号寻找答案的人,例如C#3.5 。 希望任何没有找到错误版本号的答案的人都会找到这个问题,然后用正确的版本号再次搜索。 这些是在撰写本文时已知的C#版本: .NET 1.0和VS2002发布的C#1.0 (2002年1月) C#1.2 (奇怪的是); 随.NET 1.1和VS2003发布(2003年4月)。 在调用IDisposa

What is the difference between const and readonly?

const和readonly之间有什么区别,你使用一个吗? Apart from the apparent difference of having to declare the value at the time of a definition for a const VS readonly values can be computed dynamically but need to be assigned before the constructor exits.. after that it is frozen. 'const's are implicitly static . You use a ClassName.ConstantName notation to access them. There is a su

const和readonly有什么区别?

const和readonly之间有什么区别,你使用一个吗? 除了明显的区别之外 必须在定义const时声明值VS readonly值可以动态计算,但需要在构造函数退出之前分配..在此之后它被冻结。 'const'是隐式static 。 您使用ClassName.ConstantName表示法来访问它们。 有一个微妙的区别。 考虑一下AssemblyA定义的类。 public class Const_V_Readonly { public const int I_CONST_VALUE = 2; public readonly int I_RO_VAL

sizeof(int) on x64?

When I do sizeof(int) in my C#.NET project I get a return value of 4. I set the project type to x64, so why does it say 4 instead of 8? Is this because I'm running managed code? There are various 64-bit data models; Microsoft uses LP64 for .NET: both longs and pointers are 64-bits (although traditional C-style pointers don't exist .NET). Contrast this with ILP64 where ints are also 6

sizeof(int)在x64上?

当我在C#.NET项目中执行sizeof(int) ,我得到的返回值为4.我将项目类型设置为x64,那么为什么会说4而不是8? 这是因为我正在运行托管代码? 有各种64位数据模型; Microsoft使用LP64 for .NET:长整型和指针都是64位(尽管传统的C型指针不存在.NET)。 将这与ILP64进行对比,其中整数也是64位。 因此,在所有平台上, int是32位, long是64位; 您可以在底层类型System.Int32和System.Int64的名称中看到此内容。 关键字in

Should I use int or Int32

In C#, int and Int32 are the same thing, but I've read a number of times that int is preferred over Int32 with no reason given. Is there a reason, and should I care? ECMA-334:2006 C# Language Specification (p18): Each of the predefined types is shorthand for a system-provided type. For example, the keyword int refers to the struct System.Int32 . As a matter of style, use of the keyword

我应该使用int还是Int32

在C#中, int和Int32是相同的东西,但我读过很多次int比Int32更Int32 ,没有任何理由。 有没有理由,我应该关心吗? ECMA-334:2006 C#语言规范(p18): 每种预定义类型都是系统提供类型的简写。 例如,关键字int指的是结构System.Int32 。 就风格而言,使用关键字比使用完整的系统类型名称更受青睐。 这两者确实是同义词; int会更加熟悉, Int32使32位对读取代码更加明确。 我会倾向于在我只需要'整数'的

Passing Objects By Reference or Value in C#

In C#, I have always thought that non-primitive variables were passed by reference and primitive values passed by value. So when passing to a method any non-primitive object, anything done to the object in the method would effect the object being passed. (C# 101 stuff) However, I have noticed that when I pass a System.Drawing.Image object, that this does not seem to be the case? If I pass a

在C#中通过引用或值传递对象

在C#中,我一直认为非原始变量是通过引用传递的,原始值是通过值传递的。 所以当传递给方法的任何非原始对象时,对方法中的对象做的任何事情都会影响被传递的对象。 (C#101的东西) 但是,我注意到,当我传递一个System.Drawing.Image对象时,这似乎并不是这种情况? 如果我将system.drawing.image对象传递给另一个方法,并将图像加载到该对象上,那么让该方法超出范围并返回调用方法,该图像不会加载到原始对象上?

Improve INSERT

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop application. We have large amounts of configuration data stored in XML files that are parsed and loaded into an SQLite database for further processing when the application is initialized. SQLite is

改进INSERT

优化SQLite是非常棘手的。 C应用程序的批量插入性能可以从每秒85次插入到每秒超过96,000次插入! 背景:我们使用SQLite作为桌面应用程序的一部分。 我们有大量的配置数据存储在XML文件中,这些数据被解析并加载到SQLite数据库中,以便在初始化应用程序时进一步处理。 SQLite非常适合这种情况,因为它速度很快,不需要专门的配置,数据库作为单个文件存储在磁盘上。 理由:最初我对我所看到的表现感到失望。 事实证明,根

What is the difference between casting and coercing?

I've seen both terms be used almost interchangeably in various online explanations, and most text books I've consulted are also not entirely clear about the distinction. Is there perhaps a clear and simple way of explaining the difference that you guys know of? Type conversion (also sometimes known as type cast ) To use a value of one type in a context that expects another. Noncon

强制和强制有什么区别?

我已经看到这两个术语在各种在线解释中几乎可以互换使用,并且我所咨询的大多数教科书也不完全清楚这些区别。 是否有解释你们所知道的差异的明确而简单的方法? 类型转换 (有时也被称为类型转换 ) 在期望另一个上下文的情况下使用一种类型的值。 非转换类型转换 (有时称为类型双关语 ) 一个不会改变基础位的改变。 强迫 当周围环境需要第二种类型时,编译器自动将一种类型的值转换为另一种类型的值的过程。

How do I iterate over an enum?

How can you enumerate an enum in C#? Eg the following code does not compile: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { DoSomething(suit); } } And gives the following compile-time error: 'Suit' is a 'type' but is used like a 'variable' It fail

我如何迭代枚举?

你如何enum C#中的enum ? 例如下面的代码不能编译: public enum Suit { Spades, Hearts, Clubs, Diamonds } public void EnumerateAllSuitsDemoMethod() { foreach (Suit suit in Suit) { DoSomething(suit); } } 并给出以下编译时错误: “西装”是一种“类型”,但用作“变量” 它在Suit关键字上失败,第二个失败。 foreach (Suit suit in Enum.GetValues(typeof(Suit))) {