首页 > TAG信息列表 > implicit-conversion

将VB转换为C#-类型转换

我需要帮助将某些代码从VB转换为C#. Public Function ToBase36(ByVal IBase36 As Double) As String Dim Base36() As String = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9&q

C#-是运算符-检查所有可用转换的可铸性

进一步阅读后进行编辑,修改后的问题更具体. 按照Microsoft documentation: An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown. Otherwise,

嵌套隐式运算符

我有一个名为Optional<的自定义类. T>.此类重写隐式运算符,如下所示: public static implicit operator Optional<T>(T value) { return new Optional<T>(value); } public static implicit operator T(Optional<T> value) { return value.value; } 好的,到目前为止,可以

隐式转换0到枚举

在C#中,可以将十进制文字0隐式转换为枚举(或其基础类型为枚举的可空值). C# spec,GitHub上的当前版本 An implicit enumeration conversion permits the decimal_integer_literal 0 to be converted to any enum_type and to any nullable_type whose underlying type is an

条件表达式缺少Java错误?

使用方法test1()和test2(),我收到类型不匹配错误:无法从null转换为int,这是正确的;但是为什么我在方法test3()中没有得到相同的结果?在这种情况下,Java如何评估条件表达式? (很明显,在运行时将出现NullPointerException).它是缺少的错误吗? public class Test { public int test1(

c-如何定义运算符,以便可以将用户定义类型的数组转换为原始类型的数组?

我提供以下代码来说明我的问题: #include <vector> struct Complex { int a, b, c; Complex() : a(3), b(4), c(10) {} operator int() const { return a+b+c; } }; int main() { Complex abc; int value = (abc); Complex def; def.a = 20; in

c – 没有可行的从std :: function到bool的转换

C 11 std :: function应该实现operator bool() const,那为什么clang告诉我没有可行的转换? #include <functional> #include <cstdio> inline double the_answer() { return 42.0; } int main() { std::function<double()> f; bool yes = (f = the_answer);

c – 可以将重载运算符重构为非成员函数中断任何代码吗?

考虑具有重载加法运算符=和的遗留类模板 template<class T> class X { public: X() = default; /* implicict */ X(T v): val(v) {} X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; } X<T> operator+ (X<T>

c – 我什么时候可以使用显式操作符bool而不使用强制转换?

我的班级明确转换为bool: struct T { explicit operator bool() const { return true; } }; 我有一个例子: T t; 要将它分配给bool类型的变量,我需要编写一个强制转换: bool b = static_cast<bool>(t); bool b = bool(t); bool b(t); // converting initialiser bool b{stat

使用==比较整数和字符串时JavaScript中的隐式数据类型转换

编码: var num = 20; if(num == "20") { alert("It works"); } else { alert("Not working"); } 问题: >在C编程中,我们有一个规则名称数据类型提升,当存在混合数据类型时(例如:添加整数和浮点),整数将在执行加法之前首先转换为浮点. >上面的代码将提示我一个警告框,其中显

c – 如何在size_t上使用余数运算符得到负余数?

请考虑以下代码示例: #include <iostream> #include <string> int main() { std::string str("someString"); // length 10 int num = -11; std::cout << num % str.length() << std::endl; } 在http://cpp.sh上运行此代码,结果得到5,而我期望它为-1.

c三元运算符转换理解

我无法理解这个三元运算符的转换逻辑(这里是一个例子): #include <iostream> #include <typeinfo> #include <unistd.h> #include <cxxabi.h> #include <climits> template<typename T> struct singletime { private: T value; public:

c – 为类模板忽略用户定义的转换运算符(非模板不是这样)

这段代码确实编译(重要的一点是F()只接受As,并且由于存在从B到A的隐式转换,我可以很容易地将B传递给它.) struct A {}; struct B { constexpr operator A () const {return {};} }; void F (A a) {} int main() { F(B()); return 0; } 但模板化版本无法编译: temp

c – 隐式转换将signedness’int’更改为’unsigned int’

我正在使用clang来编译程序,我需要在clang中编译它而没有错误.我没有得到其他编译器的错误. 代码中的错误行是 memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants); 整个功能是这样的: Ocean::Ocean(int num_boats, int x_quadrants, int y_quadrants) { grid_ = new

c – 通过const char *构造函数将false转换为object

我构建了以下最小的示例: class A { public: A(const char *s); private: const char *p; }; A::A(const char *s) : p(s) { } A foo() { return false; } A bar() { return true; } 用g编译(Debian 4.7.2-5)4.7.2我得到以下内容: t.cc: In

C中的隐式转换

在C中,我试图使用条件运算符进行隐式转换.考虑这个例子: class MyFloat { public: MyFloat(float val){m_val = val;} operator float(){return m_val;} protected: float

为什么可以在C中写出字符串s =“xxx”这样的表达式?

我经常看到: string str = "123"; 我的问题是,如果string是一个类类型,为什么我们可以直接将它设置为“123”而不是使用new或直接初始化它? 我习惯看到类似的东西 classType *pt = new classType; classType pt = classType(); 不 classType s = value; 解决方法:C字符串类型有

java – 为什么在使用=将一个整数连接到一个字符串时g不会发出警告/错误

我有这个代码: #include <iostream> using namespace std; int main() { string name = "John ";

c# – 如何将接口的隐式转换写入另一种类型?

我想尝试做类似下面的事情: public class SomeWrapper : ISomeWrapper{ public static implicit operator ActualRec(ISomeWrapper someWrapper) { return ((SomeWrapper)someWrapper).SomeInfo; } } 但是这段代码失败了,说: “参数或返回类型必须

c# – Moq设置不适用于方法调用,后跟隐式转换

对于这个问题,考虑(创建)接口: public interface ITestMe { string TakeInt64(long x); } 然后运行以下代码: public void Test() { var mock1 = new Mock<ITestMe>(MockBehavior.Strict); Expression<Func<ITestMe, string>> expr1 = x => x.TakeInt64(2); Conso

c# – 当使用的值可以隐式转换为这些结构时,为什么我不能使用带有两个结构的重载==?

我注意到在应用operator ==时,用户定义的隐式转换行为与用户定义的隐式转换到任意结构MyStruct的行为之间存在差异. 如果我有: public struct IntA { public IntA(int value) { m_value = value; } public static implicit operator int(IntA a) { return a.m_val