Tag: 类型转换

C# – 将float转换为int …并根据余数更改int

这可能是一个非常新手的问题(好吧,我很确定它是),但是我有一个浮动的东西正在返回,我需要一种快速有效的方法将它变成一个int 。 很简单,但我有一个例外。 如果float的其余部分不是.0,那么我想增加int 。 一些简单的例子: Float = 98.0,Int = 98 Float = 98.1,Int = 99 Float = 6.6,Int = 7 等等

即使签名匹配,也无法将一种类型的委托分配给另一种类型

我的病态好奇让我想知道为什么以下失败: // declared somewhere public delegate int BinaryOperation(int a, int b); // … in a method body Func addThem = (x, y) => x + y; BinaryOperation b1 = addThem; // doesn’t compile, and casting doesn’t compile BinaryOperation b2 = (x, y) => x + y; // compiles!

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也是一个值类型,不是吗?

如何查找和调用特定类型的.Net TypeConverter?

我想实现一个通用的运行时类型转换函数,它使用.Net TypeConverters来进行转换。 有谁知道如何查找和调用特定类型的TypeConverter? 考虑一下这个C#示例: // // Convert obj to the type specified by ‘toType’. // object ConvertTo(object obj, Type toType) { if (TypeIsEqualOrDerivesFrom(obj.GetType(), toType)) <– I know how to implement this. { // The type of obj is the same as the type specified by 'toType' or // the type of obj derives from the […]

WPF – 将Bitmap转换为ImageSource

我需要将System.Drawing.Bitmap转换为System.Windows.Media.ImageSource类,以便将其绑定到WizardPage(扩展WPF工具包)的HeaderImage控件中。 位图设置为我编写的程序集的资源。 它被引用如下: public Bitmap GetBitmap { get { Bitmap bitmap = new Bitmap(Resources.my_banner); return bitmap; } } public ImageSource HeaderBitmap { get { ImageSourceConverter c = new ImageSourceConverter(); return (ImageSource) c.ConvertFrom(GetBitmap); } } 这个转换器是我在这里找到的: http : //www.codeproject.com/Questions/621920/How-to-convert-Bitmap-to-ImageSource我得到一个NullReferenceException return (ImageSource) c.ConvertFrom(Resources.my_banner); 如何初始化ImageSource以避免此exception? 或者还有另一种方式吗? 我希望之后使用它: <xctk:WizardPage x:Name="StartPage" Height="500" Width="700" HeaderImage="{Binding HeaderBitmap}" Enter="StartPage_OnEnter" 提前感谢您的任何答案。

在C#中将二进制数的字符串表示forms转换为int

我有一个八个1和0的字符串,中间有空格,类似于“1 0 0 1 1 0 1 0”,我想要转换为int。 有一个简单的方法吗? 我觉得某种linq解析可以做到这一点,但是一旦找到它们,我甚至不知道如何处理这些角色。

Convert和Parse有什么区别?

我可以编写以下内容将对象转换为整数。 Convert.ToInt32(myObject); 但我也可以写 Int.Parse(myObject.ToString()); 有什么区别吗? 我应该使用哪一个? 提前致谢。

如何使用货币符号将字符串解析为十进制?

我不知道为什么这不起作用: string s = “12,00 €”; var germanCulture = CultureInfo.CreateSpecificCulture(“de-DE”); decimal d; if (decimal.TryParse(s, NumberStyles.AllowCurrencySymbol, germanCulture, out d)) { // i want to get to this point Console.WriteLine(“Decimal value: {0}”, d); }

无法将类型’customtype’隐式转换为’othercustomtype’

我是C#的新手。 我有一个Persons类和一个inheritance自Persons类的User类。 我的控制台我在数组中输入用户。 然后,我只需输入用户ID即可向用户arrays中的用户添加注释。 在我的Persons类中,我有这个函数,必须搜索此用户是否在users数组中。 public static Persons FindPerson(Persons[] persons, int noteid) { foreach (Persons person in persons) if (person.ID == noteid) return person; return null; } 在我的User类中,我有一个函数循环id的整个输入,直到它获得users数组中的id。 public static User SelectUser(User[] users) { while (true) { Console.Write(“Please enter the User id: “); string input = Console.ReadLine(); int id; if (int.TryParse(input, out id)) { Persons […]

Double.MaxValue到整数是否定的?

为什么Double.MaxValue转换为整数类型会导致负值,该类型的最小值? double maxDouble = double.MaxValue; // 1.7976931348623157E+308 long maxDoubleLong = (long) maxDouble; // -9223372036854775808 我理解编译器错误,如果它太大或运行时出现OverflowException ,或者如果我使用unchecked ,转换可能不会引发exception,但结果将变为未定义且不正确(否定)。 同样奇怪的是值很长long.MinValue : bool sameAsLongMin = maxDoubleLong == long.MinValue; // true 顺便说一句,如果我将它转换为int也会发生同样的情况: int maxDoubleInt = (int)maxDouble; // -2147483648 bool sameAsIntMin = maxDoubleInt == int.MinValue; // true 如果它试图将其强制转换为decimal我会在运行时获得一个OverflowException decimal maxDoubleDec = (decimal)maxDouble; // nope 更新 :看起来迈克尔和巴雷的答案在头上很明显,如果我明确地使用了checked我会得到一个OverflowException : checked { double […]