Tag: 类型

为什么编译器决定2.3是double而不是decimal?

为什么编译器决定2.3是double,所以这段代码不能编译: decimal x; x = 2.3; // Compilation error – can not convert double to decimal. x = (decimal) 2.3 // Ok 为什么编译器不这样想: 他想得到一个小数,他给我一个可以是小数的值,所以它是小数! 为什么这不会出现编译错误: short x; x = 23; // OK 谁说23不是int?

如何找到两种类型中最小的可分配类型(重复)?

这是两种使用的扩展方法 public static Type FindInterfaceWith(this Type type1, Type type2) { // returns most suitable common implemented interface } public static Type FindBaseClassWith(this Type type1, Type type2) { // returns most derivative of common base class } 如果FindInterfaceWith没有通用的实现接口,则返回null 。 如果FindBaseClassWith没有更多的派生公共基类,则返回System.Object 。 如果其中一个参数是接口,则FindBaseClassWith返回null 。 如果任何参数为null它们都返回null 。 最终解决方案中的方法签名如下: public static Type FindAssignableWith(this Type type1, Type type2) { // […]

检查类型是否实现通用接口而不考虑generics类型参数

我有一个界面 public interface MyInterface { } 实现是无关紧要的。 现在我想检查给定类型是否是该接口的实现。 这种方法失败了 public class MyClass : MyInterface { } 但我不知道怎么做检查。 public void CheckIfTypeImplementsInterface(Type type) { var result1 = typeof(MyInterface).IsAssignableFrom(type); –> false var result2 = typeof(MyInterface).IsAssignableFrom(type); –> true } 我需要做些什么才能使result1成为现实?

非非托管类型和托管类型之间有什么区别?

当我为实验目的编写以下代码段时,它引发了hover错误(参见屏幕截图): 无法声明指向非非托管类型的’动态’ 片段: dynamic* pointerToDynamic = &fields; 虽然代码显然不被允许(你不能获取托管类型的地址),但它提出了一个问题:什么是非非托管类型 ,它与托管类型有什么不同? 或者只是Visual Studio试图变得有趣?

Convert.ToBoolean以“0”值失败

我正在尝试将值”0″ ( System.String )转换为其Boolean表示forms,如: var myValue = Convert.ToBoolean(“0”); // throwing an exception here 我查看了MSDN页面 ,在代码示例块中,我找到了这些行: ConvertToBoolean(“0”); // … Unable to convert ‘0’ to a Boolean. 在我的代码中,我正在从System.String转换为Boolean如下所示: // will be OK, but ugly code var myValue = Convert.ToBoolean(Convert.ToInt32(“0”)); 有没有其他方法转换为Boolean类型没有这样丑陋的代码? 为什么会出现这种exception? 因为从引用类型System.String转换为值类型System.Boolean ,但System.Int32也是一个值类型,不是吗?

值类型inheritance自System.Object …为什么?

可能重复: .NET中的所有内容都是对象吗? ValueTypes如何从Object(ReferenceType)派生而仍然是ValueTypes? 嗨,我只是不明白。 System.Object是(我认为)引用类型,但.NET中的所有数据类型都inheritance自它。 值类型也是如此。 我不明白 – 值类型在堆栈上有它的值,但它inheritance自Object? 希望有人能帮助我理解

哪个好用:Object.GetType()== typeof(Type)或Object是Type?

我想知道哪个语句在Performance Point of View中是否有用 Object.GetType() == typeof(Type) 要么 Object is Type

如何在C#中创建自定义类型数组?

我创建了一个包含不同类型元素的对象数组: public static int a; public static string b; public static ushort c; object[] myobj = new obj[]{ a, b, c}; 如果我想创建一个包含此myobj类型数组元素的数组,我该怎么做? 我的意思是这样的: myobj[] myarray = new myobj[]; <= but to do this, myobj should be a type. 不知道如何解决这个问题。 感谢大家。

如何获取Enum描述的字符串列表?

如何获得Enum值的列表? 例如,我有以下内容: public enum ContactSubjects { [Description(“General Question”)] General, [Description(“Availability/Reservation”)] Reservation, [Description(“Other Issue”)] Other } 我需要做的是将ContactSubject.General作为参数传递,它返回描述的列表。 此方法需要使用任何枚举,而不仅仅是ContactSubject(在我的示例中)。 签名应该类似于GetEnumDescriptions(枚举值)。

将对象的通用集合传递给需要基类型集合的方法

假设我有一个期望基类型的generics集合参数的方法,请参阅下面的Test.MethodA(IEnumerable(BaseClass)listA) 。 为什么我传递代码不会构建的派生类型的集合? 难道DerivedClass的所有实例都不是BaseClass吗? 我本来可以创建一个新的List(BaseClass)并将其传递给MethodA(IEnumerable(BaseClass)listA) 。 但我认为C#足够聪明,知道DerivedClass的集合具有与BaseClass集合相同的所有属性。 使用List.Cast(T)()方法,因为我已经展示了解决此问题的最佳方法? abstract class BaseClass { public int SomeField; public abstract string SomeAbstractField { get; } } class DerivedClass:BaseClass { public override string SomeAbstractField { get { return “foo”; } } } class TestClass { public void MethodA(IEnumerable listA) { } public void MethodB() { List listB = new […]