我可以将long与int使用dynamic进行比较。但我无法将ulong与使用dynamic的int进行比较。
C#动态bug?
无例外样本
long a = 10000;
int b = 10;
dynamic c = a;
dynamic d = b;
if (c <= d)
MessageBox.Show("c");
else
MessageBox.Show("d");
异常样本
ulong a = 10000;
int b = 10;
dynamic c = a;
dynamic d = b;
if (c <= d)
考虑以下代码:
class Program
{
public static explicit operator long(Program x) { return 47; }
static int Main(string[] args)
{
var x = new Program();
Console.WriteLine((decimal) x);
}
}
令我惊讶的是,这会输出47;换句话说,即使强制转换为decimal,也会调用explicit operator long。
C#规范中有没有明确说明应该发生这种情况(如果是,具
示例代码:
long num1 = -1;
unsigned long num2 = 1;
if (num1 > num2)
printf("Num1 is greater\n");
else
printf("Num2 is greater\n");
为什么这个值将num1 > num2计算为true而不是false?
我看到了这个问题:
src/planning.cpp:892:95: error: no matching function for call to ‘std::vector<std::pair<pcl::PointXYZ, cv::Point_<float> > >::vector(int, std::pair<pcl::PointXYZ, int>)’
std::vector<std::pair<PointT,cv::Point2f> > _candidate_points(3,make_pair(Po
我刚发现这个:
ushort i = 4;
i = i + 4;
给出一个编译器错误:
不能隐式地将类型'int‘转换为'ushort’。存在显式转换(是否缺少强制转换?)
因此,我必须解决这个问题:
ushort i = 4;
i = (ushort)(i + 4);
背后的原因是什么?难道使用所有数据类型不是很明显吗?
我使用以下代码来签名扩展从1.5字节到16位的12位值:
word[0] |= ((word[0] & 0x800) != 0 ? (Int16)(-4096) : (Int16)0);
如果不将最后零转换为Int16,编译器会发出以下抱怨:
Warning 1 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
Error 2 Cannot implicitly convert type 'int' to
我知道这个错误:
不能隐式地将'ulong‘转换为'bool’
在这里(u*u) for (ulong u = 2; u * u; u++)
下面的代码块。
static bool IsPrime(ulong Num)
{
if (Num < 2) return false;
else if (Num < 4) return true;
else if (Num % 2 == 0) return false;
for (ulong u = 2; u * u; u++)
if (Num % u == 0
字节的表示可以声明如下所示的字节:
byte myByte = 255;
那就是
在前面的声明中,整数文本255被隐式地从int转换为字节。如果整数文字超过字节范围,则会发生编译错误。
因此,我很难理解为什么下面的编译错误是“无法隐式地将”int“类型转换为”字节“)
byte value = on ? 1 : 0; // on is defined as a bool earlier
如果这有什么不同的话,我将在VS 2012上编译为Windows应用程序项目。
为什么我不能使用下面的代码?
ulong test(int a, int b)
{
return a == b ? 0 : 1;
}
它向我展示:
Cannot implicitly convert type 'int' to 'ulong'. An explicit conversion exists (are you missing a cast?)
下列措施将起作用:
ulong test(int a, int b)
{
return false ? 0 : 1;
}
我知道如何解决这个问题。我只想知道原因。
谢谢。
我是C#的新手。
首先,我可以问一下int是否是C#中整数的默认类型?换句话说,整数的文字在C#中是否被视为int?
如果是这样的话,因为我们总是可以像下面这样声明一个字节:
byte b = 1;
我可以问一下,在初始化过程中是否发生了隐式类型转换(我的猜测是:文字1首先被视为int,然后被转换为byte)?
如果上面的猜测是正确的,那么我可以问为什么允许我们使用上面的文字隐式地将int转换为byte,而当文本被int类型的变量替换时却不能这样做(例如,如下所示)?
int a = 1;
byte c = a; // error
此外,我发现对于常量:
const int d = 1;
by
first_expression和second_expression的类型必须相同,或者必须存在从一种类型到另一种类型的隐式转换。
如果文字没有后缀,它有第一种类型,在这些类型中它的值可以表示: int、uint、long、ulong。
考虑:
var value = test ? (Int64)1 : 0;
0,没有后缀的十进制数字文字将被转换为int.int可以隐式转换为Int64。由于这种转换只发生在一个方向上,所以我们可以放心地看到返回值将是一个Int64。
然而:
var value = test ? (UInt64)1 : 0;
UInt64和int不能相互隐式
我得到了一个错误:
CS0034 C#运算符'-‘在'long’和'ulong‘类型的操作数上是不明确的
我试过好几种方法,但都没能修好。
这里我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
namespac
我需要编写一个函数,如果两个数字的乘法大于ULONG_MAX限制,则返回ULONG_MAX。否则返回false。
我尝试了以下方法:
bool isGtThanULONG_MAX(double A, double B) {
double result = A * B;
if (result > ULONG_MAX)
return true;
else
{
//If this could be due to overflow, then again check:
double temp = result / A
我有一个关于隐式类型转换的问题
为什么这种隐式类型转换在C#中有效?我了解到隐式代码通常是不起作用的。
我在这里有一个关于隐式类型转换的代码示例
char c = 'a';
int x = c;
int n = 5;
int answer = n * c;
Console.WriteLine(answer);
首先,我想说,这只是一个简单的代码,这是一个例子,我正在学习一个考试。
public class TOblik
{
public int povrsina = 0;
public TVrsta vrsta = 0;
public TOblik(TVrsta a)
{
}
}
public enum TVrsta
{
Kvadrat,
Krug
}
public class A
{
public stat
我目前有一个泛型类,它允许使用表达式作为值。
public class Expression<T>
{
public T Value { get; set; }
public string ExpressionText { get; set; }
public static implicit operator Expression<T>(string input)
{
if (string.IsNullOrEmpty(input))
return null;
if (input.
基于这个有趣的问题:和 中提到的不断折叠,我偶然发现编译器似乎不一致的行为:
考虑以下代码片段:
int i = 1;
uint j = 2;
var k = i - j;
在这里,编译器正确地将k解析为long。这个特定的行为在规范中得到了很好的定义,正如前面提到的问题的答案所解释的那样。
令我惊讶的是,通常处理文字常量或常量时,行为会发生变化。读了尼古拉斯·凯里的“”,我意识到这种行为可能是不一致的,所以我检查并确定了:
const int i = 1;
const uint j = 2;
var k = i - j; //Compile time error: The operation
public class Program
{
public static void Main(string[] args)
{
char x = 'A';
int i = 0;
Console.WriteLine (true ? x : 0);
Console.WriteLine(false ? i : x);
}
}
我在谷歌上找不到任何东西,有人请解释一下为什么输出是65,65
我目前正在制作一个程序,监控网络上服务器的正常运行时间和大小。在显示服务器的大小和剩余空间时,我遇到了一些问题。
我的代码是
public void setSpace(string ip)
{
ManagementScope scope = new ManagementScope("\\\\" + ip + "\\root\\cimv2");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem"
我尝试使用以下代码
double d;
double? a = double.TryParse("3.14", out d) ? d : null;
但它不会编译为“double和null之间没有隐式转换”。将上面的代码拆分如下:
double d;
double? a;
if ( double.TryParse("3.14", out d))
a = d;
else
a = null;
为什么在使用?-运算符时会有区别?
在C#中,运算符将根据结果的大小隐式地缩小和扩大结果变量。
UInt32 exampleA = 10000000/2; // Will return a UInt32
UInt32 exampleB = 1000000000000/2; // Will implicitly widen to UInt64 and cause a compile error
UInt64 exampleC = 1000000000000/2; // Will implicitly widen to UInt64 without error
你能为一种方法做这件事吗?如果是的话,怎么做?我认为它
在Java中,您可以轻松地向char数组添加int值,而无需像这样定义它
String name = " maisam is my name ";
char[] arr = name.toCharArray();
for (int i = 0; i < arr.length; i++) {
arr[i] += 100;
}
但是在c#中,您需要在增加价值的同时提及(char)
string name = " maisam is my name ";
char[]
下面的代码不是用
错误CS0029:不能隐式地将'CSConsoleTest.Derived‘转换为'T’。
这是一个编译器错误,或者原因是什么?
public class Base
{
public Derived Derived;
}
public class Derived : Base
{
}
class Program
{
public static void Func<T>(T obj) where T : Base
{
obj = obj.Derived;
}
}