implicit-cast

Why is implicit conversion allowed from superclass to subclass?

无人久伴 提交于 2019-12-07 05:34:13
问题 Can someone tell me why the line with "//Compiles" compiles, and why the line with "//Doesn't Compile" does not? I don't understand why A would be implicitly convertible to B, not the other way round. public class SomeClass { static public void Test() { AClass a = new AClass(); BClass b = new BClass(); a = b; // Compiles b = a; // Doesn't compile } } public class AClass { public void AMethod() { Console.WriteLine("AMethod"); } } public class BClass : AClass { public void BMethod() { Console

C# Implicit operator with generic

梦想与她 提交于 2019-12-06 07:01:51
问题 I'm writing an abstract wrapper for enum in C # (I want something like enum in Vala). My code is: public abstract class Wraper<T, TE> where T : Wraper<T, TE>, new() { public TE Value; public static implicit operator T(TE value) { return new T() { Value = value }; } public static implicit operator TE(T value) { return value.Value; } } I want to do with something like this: public enum EFoo { A, B, C, D, E}; public class Foo : Wraper<Foo, EFoo> { public bool IsBla { get { return Value == EFoo.A

Implicit cast operator and the equality operator

假如想象 提交于 2019-12-06 01:43:11
Say I have a simple object which supports implicit casting to System.String public sealed class CompanyCode { public CompanyCode(String value) { // Regex validation on value format _value = value; } private readonly String _value; public override String ToString() { return _value; } static public implicit operator String(CompanyCode code) { if(code == null) return null; return code.ToString(); } } Now lets say in another part of my program I perform a comparison with a string: var companyCode = { some company code object } if (companyCode == "MSFTUKCAMBS") // do something... What is the

Implicit array casting in C#

倾然丶 夕夏残阳落幕 提交于 2019-12-05 12:17:59
I have the following classes with an implicit cast operator defined: class A { ... } class B { private A m_a; public B(A a) { this.m_a = a; } public static implicit operator B(A a) { return new B(a); } } Now, I can implicitly cast A to B. But why can't I implicitly cast A[] to B[] ? static void Main(string[] args) { // compiles A a = new A(); B b = a; // doesn't compile A[] arrA = new A[] {new A(), new A()}; B[] arrB = arrA; } Thanks, Malki. As Mehrdad Afshari mentioned, you're out of luck doing this implicitly. You'll have to get explicit, and it'll involve an array copy. Thankfully, you can

Why is implicit conversion allowed from superclass to subclass?

最后都变了- 提交于 2019-12-05 09:54:36
Can someone tell me why the line with "//Compiles" compiles, and why the line with "//Doesn't Compile" does not? I don't understand why A would be implicitly convertible to B, not the other way round. public class SomeClass { static public void Test() { AClass a = new AClass(); BClass b = new BClass(); a = b; // Compiles b = a; // Doesn't compile } } public class AClass { public void AMethod() { Console.WriteLine("AMethod"); } } public class BClass : AClass { public void BMethod() { Console.WriteLine("BMethod"); } } thanks! Because B does everything that A does but A does not necessarily do

How to cast implicitly on a reflected method call

人走茶凉 提交于 2019-12-05 09:24:46
问题 I have a class Thing that is implicitly castable from a string . When I call a method with a Thing parameter directly the cast from string to Thing is done correctly. However if I use reflection to call the same method it throws the exception System.ArgumentException : Object of type 'System.String' cannot be converted to type 'Things.Program+Thing'. Maybe there is a good reason for this, but I can't figure it out. Does somebody have an idea how to get this working using reflection? namespace

Implicit casting Integer calculation to float in C++

亡梦爱人 提交于 2019-12-04 16:43:38
Is there any compiler that has a directive or a parameter to cast integer calculation to float implicitly. For example: float f = (1/3)*5; cout << f; the "f" is "0", because calculation's constants(1, 3, 10) are integer. I want to convert integer calculation with a compiler directive or parameter. I mean, I won't use explicit casting or ".f" prefix like that: float f = ((float)1/3)*5; or float f = (1.0f/3.0f)*5.0f; Do you know any c/c++ compiler which has any parameter to do this process without explicit casting or ".f" thing? If you don't like either of the two methods you mentioned, you're

implicit operator

北城以北 提交于 2019-12-04 00:12:38
I just saw it was using in one of the recent answers: public static implicit operator bool(Savepoint sp) { return sp != null; } Why do we need word implicit here, and what does it mean? Implicit means that the conversion doesn't require a cast in your code. You can now do this: Savepoint point = new Savepoint(); if(point) // becomes a bool using your operator { } instead of having to do this: Savepoint point = new Savepoint(); if((bool)point) // an "explicit" conversion { } One example of why this is a useful distinction is numeric types. There's an implicit conversion from "smaller" types to

How to cast implicitly on a reflected method call

只愿长相守 提交于 2019-12-03 23:44:17
I have a class Thing that is implicitly castable from a string . When I call a method with a Thing parameter directly the cast from string to Thing is done correctly. However if I use reflection to call the same method it throws the exception System.ArgumentException : Object of type 'System.String' cannot be converted to type 'Things.Program+Thing'. Maybe there is a good reason for this, but I can't figure it out. Does somebody have an idea how to get this working using reflection? namespace Things { class Program { public class Thing { public string Some; public static implicit operator

Which Json deserializer renders IList<T> collections?

六眼飞鱼酱① 提交于 2019-12-03 04:01:58
I'm trying to deserialize json to an object model where the collections are represented as IList<T> types. The actual deserializing is here: JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<IList<Contact>>( (new StreamReader(General.GetEmbeddedFile("Contacts.json")).ReadToEnd())); Before i post the exception i'm getting you should know what the implicit conversions are. This is the Contact type: public class Contact { public int ID { get; set; } public string Name { get; set; } public LazyList<ContactDetail> Details { get; set; } //public List