const vs. static readonly

Possible Duplicate: What is the difference between const and readonly? So from what I read, in C#, const and static readonly will both make a value unalterable during the execution of a program. However, const should be used with quantities which are unlikely to ever change (eg pi, radius of earth, litters per gallon etc.). On the other hand, static readonly should be used with values tha

常量与静态只读

可能重复: const和readonly有什么区别? 因此,从我读的内容来看,在C#中, const和static readonly将在执行程序期间使值不可更改。 但是, const应该用于不可能改变的数量(例如pi,地球半径,每加仑垃圾等)。 另一方面,应该使用static readonly值,这些值当前是恒定的,但将来可能会/将会改变(例如软件版本,算法中的乘数等)。 我说得对吗? 我不知道你的第二个项目(我可能会使用常量来表示软件版本或算法

Reflecting parameter name: abuse of C# lambda expressions or Syntax brilliance?

I am looking at the MvcContrib Grid component and I'm fascinated, yet at the same time repulsed, by a syntactic trick used in the Grid syntax: .Attributes(style => "width:100%") The syntax above sets the style attribute of the generated HTML to width:100% . Now if you pay attention, 'style' is nowhere specified, is deduced from the name of the parameter in the expression! I had

反映参数名称:滥用C#lambda表达式或语法辉煌?

我正在查看MvcContrib Grid组件,并且我着迷于它,但同时还击退了Grid语法中使用的语法技巧: .Attributes(style => "width:100%") 上面的语法将生成的HTML的样式属性设置为width:100% 。 现在,如果您注意,'风格'无处指定,则从表达式中的参数名称推断出来! 我不得不深入研究并找到'魔法'发生的地方: Hash(params Func<object, TValue>[] hash) { foreach (var func in hash)

Get property value from string using reflection in C#

I am trying implement the Data transformation using Reflection1 example in my code. The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties and have GetSourceValue get the value of the property using only a single string as the parameter. I want to pass a class and property in the string and resolve the value of the property. Is this

使用C#中的反射从字符串获取属性值

我想在我的代码中使用Reflection1示例实现数据转换。 GetSourceValue函数有一个用于比较各种类型的开关,但我想删除这些类型和属性,并使GetSourceValue仅使用一个字符串作为参数来获取该属性的值。 我想传递字符串中的类和属性并解析属性的值。 这可能吗? 1原始博客帖子的Web Archive版本 public static object GetPropValue(object src, string propName) { return src.GetType().GetProperty(propName).GetVal

Find a private field with Reflection?

Given this class class Foo { // Want to find _bar with reflection [SomeAttribute] private string _bar; public string BigBar { get { return this._bar; } } } I want to find the private item _bar that I will mark with a attribute. Is that possible? I have done this with properties where I have looked for an attribute, but never a private member field. What are

用Reflection找到一个私人领域?

鉴于这个班级 class Foo { // Want to find _bar with reflection [SomeAttribute] private string _bar; public string BigBar { get { return this._bar; } } } 我想找到私人项目_bar,我将用一个属性标记。 那可能吗? 我已经用属性查找属性,但从来没有私人成员字段。 我需要设置哪些绑定标志来获取私有字段? 使用BindingFlags.NonPublic和BindingFlags.Instance标志FieldInfo[] f

Joining two lists together

If I have two lists of type string (or any other type), what is a quick way of joining the two lists? The order should stay the same. Duplicates should be removed (though every item in both links are unique). I didn't find much on this when googling and didn't want to implement any .NET interfaces for speed of delivery. You could try: List<string> a = new List<string>()

将两个列表结合在一起

如果我有两个字符串类型(或任何其他类型)的列表,那么加入这两个列表的快捷方式是什么? 订单应该保持不变。 应删除重复项目(尽管两个链接中的每个项目都是唯一的)。 在Google上搜索时,我没有发现太多内容,也不希望为了交付速度而实现任何.NET接口。 你可以尝试: List<string> a = new List<string>(); List<string> b = new List<string>(); a.AddRange(b); AddRange MSDN页面 这保

When does it matter whether you use int versus Int32, or string versus String?

In C#, the keywords for built-in types are simply aliases for corresponding types in the System namespace. Generally, it makes no difference whether you use a keyword (such as int ) or an identifier (such as Int32 ) to refer to a built-in type. But there's an exception to everything, so my question in two parts is: When does C# require you to use, or not use, a keyword? When does using

什么时候使用int还是Int32还是string与String?

在C#中,内置类型的关键字只是System命名空间中对应类型的别名。 通常,使用关键字(如int )还是使用标识符(如Int32 )来引用内置类型都没有区别。 但是有一个例外,所以我的问题分两部分: C#何时需要您使用或不使用关键字? 什么时候使用关键字而不是标识符来改变程序的含义? C#不要求您使用其中一个或另一个,因为它们是等效的。 这是个人喜好和编码习惯。 因此,请使用看起来对您和您的团队更具可读性的文

What is the difference between bool and Boolean types in C#

C#中bool和Boolean类型有什么区别? bool is an alias for System.Boolean just as int is an alias for System.Int32 . See a full list of aliases here: Built-In Types Table (C# Reference). I don't believe there is one. bool is just an alias for System.Boolean They are one in the same. bool is just an alias for Boolean.

C#中bool和布尔类型有什么区别

C#中bool和Boolean类型有什么区别? bool是System.Boolean的别名,就像int是System.Int32的别名一样。 查看完整的别名列表:内置类型表(C#参考)。 我不相信有一个。 bool只是System.Boolean的别名 他们是一样的。 布尔只是布尔的别名。

What is 'long?' data type?

I am going over some code written by another developer and am not sure what long? means: protected string AccountToLogin(long? id) { string loginName = ""; if (id.HasValue) { try {.... long is the same as Int64 long data type The ? means it is nullable A nullable type can represent the normal range of values for its underlying value type, plus an additional null val

什么是“长”? 数据类型?

我正在阅读另一个开发人员编写的代码,不知道多long? 手段: protected string AccountToLogin(long? id) { string loginName = ""; if (id.HasValue) { try {.... long与Int64相同 长数据类型 这个? 意味着它是可空的 可为空的类型可以表示其基础值类型的正常值范围,以及一个附加的空值 可为空的类型 可空例子: int? num = null; if (num.HasValue == true) { System.Console.WriteLi

What is the difference between char s[] and char *s?

In C, one can use a string literal in a declaration like this: char s[] = "hello"; or like this: char *s = "hello"; So what is the difference? I want to know what actually happens in terms of storage duration, both at compile and run time. The difference here is that char *s = "Hello world"; will place "Hello world" in the read-only parts of the memory, and making s a pointer t

char s []和char * s有什么区别?

在C中,可以在像这样的声明中使用字符串字面值: char s[] = "hello"; 或者像这样: char *s = "hello"; 那么区别是什么呢? 我想知道在编译和运行时在存储时间方面实际发生了什么。 这里的区别在于 char *s = "Hello world"; 将放置"Hello world"在内存的只读部分,使s的指针,使得在这个内存非法任何写入操作。 在做: char s[] = "Hello world"; 将文字字符串放入只读存储器,并将该字符串复制到堆栈

Multiline String Literal in C#

Is there an easy way to create a multiline string literal in C#? Here's what I have now: string query = "SELECT foo, bar" + " FROM table" + " WHERE id = 42"; I know PHP has <<<BLOCK BLOCK; Does C# have something similar? You can use the @ symbol in front of a string to form a verbatim string literal: string query = @"SELECT foo, bar FROM table WHERE id = 42"; You also do n

C#中的多行字符串

有没有一种简单的方法在C#中创建多行字符串文字? 这是我现在拥有的: string query = "SELECT foo, bar" + " FROM table" + " WHERE id = 42"; 我知道PHP有 <<<BLOCK BLOCK; C#有类似的东西吗? 你可以在string前面使用@符号来形成一个逐字字符串文字: string query = @"SELECT foo, bar FROM table WHERE id = 42"; 在使用此方法时,您也不必转义特殊字符,但双引号除外,如Jon Skeet的答案中所示。