How can I solve this error? msg?
static public class Blah { public static T val<T>(this bool b, T v) { return b == true? v:0; } }
Error
Type of conditional expression cannot be determined because there is no implicit conversion between 'T' and 'int
In C # there is no way to do this. You can do
where T: struct and make T be a value type, but that is still not enough.
where T: struct
Or you can do
default(T)which is 0 when T is int.
default(T)
If you want T to be int, accept int rather than T. Otherwise, consider returning the default value (T) if b == false.
return b ? v : default(T);
If T is int, it will return 0. If it is a reference type, it will be zero. And further and further ..
generics, int?
// No need to compare b to true... public static int val(this bool b, int v) { return b ? v : 0; }
default(T), .
public static T val<T>(this bool b, T v) { return b ? v : default(T); }
default(T) 0 int , false bool s, null ...
int
false
bool
null
T:
public static T val<T>(this bool b, T v) { return b == true? v : default(T); }
v : 0 v : default(T), . int, .
v : 0
v : default(T)
T , :
public static int val(this bool b, int v) { return b ? v : 0; }
, , :
public static int val<T>(this bool b, T v) where T : struct { return b ? v : default(T); }
Srsly!
" T int", , strong-typing:
strong-typing
static public class Blah { public static int val(this bool b, int v) { return b == true? v:0; } }
!:)
Source: https://habr.com/ru/post/1742497/More articles:What happened to this line? - stringError passing a template function as an argument to another function in C ++ - c ++How to map a database view using Ruby ActiveRecord? - ruby | fooobar.comWhy switch statements continue after a case - javaHow can I access network shares and connections? - windowsThe C # property is exactly the same as defined in two places - inheritanceС#: Как удалить элементы из коллекции IDictionary> с LINQ? - collectionsRegex: find / replace all substrings without a given string before? - regexSharing on Facebook from a public kiosk - user cannot log out of FB! - facebookIntelliJ IDEA Tomcat Manager application - intellij-ideaAll Articles