Implicit cast operator and the equality operator - c#

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;
}
readonly String _value;
public override String ToString() => _value;
static public implicit operator String(CompanyCode code) =>
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 compiler doing with the == operator? Is it implicitly casting companyCode to a string and running the System.String == implementation? Is it using the System.Object == implementation? Or will the compiler just complain at me? (I don't have a compiler to check this right now).
As far as I can see I have a couple of other options.
Implement an ==(String x) operator on CompanyCode.
Implement the IEquatable<String> interface on CompanyCode.
Would any (or both) of these options be preferable?

Say I have a simple object which supports implicit casting to System.String
I would question this design decision to start with. The fact that it's brought up this question of operator overloading suggests that your colleagues will be asking the same sort of questions. I don't even know the answer (in terms of what the compiler will do) off the top of my head.
I definitely wouldn't suggest implementing IEquatable<string>, as then x.Equals(y) won't be symmetric with y.Equals(x). You could implement two overloads of == in your class, both ways round... but then it wouldn't be consistent with Equals.
I would suggest just having a property called Value or Code of type string, then you can use:
if (companyCode.Value == "MSFTUKCAMBS")
and it will be immediately clear what that means.
Basically, I think the situations where implicit conversions are appropriate are very few and far between.
From the Design Guidelines for Class Library Developers
Do not provide a conversion operator if such conversion is not clearly expected by the end users.
Is there such a clear expectation here?

It will implicitly cast to a string and check equality using the string's == operator.
For the case you show - every way you offered is suitable, but every way has a different purpose and meaning.
Implicitly conversion should usually be avoided.
Implementing the == is to allow comparing with a string,
and IEquatable is simply to allow using the class as type IEquatable, for outside code references. The IEquatable may very well just return the == result.
For this case, I would choose the == operator overloading, unless you have any other purpose for using implicit conversion.
Also, if you use implicit conversions, it will be a bit uglier code-wise but smarter robust-wise to overload an EXPLICIT conversion, and not an implicit one, so whenever one wants to convert your class to a string, he will have to cast it using (string)obj, which is also a good reminder for whats really happening in the code.

I personally would suggest do not use operator overloading, in these cases. It's kind of confusing looking on the code, to understand what is going on.
It's a much better, imo, having some function that esplicitly manifests comparison operation, or, like Jon suggests, use a property.
In short make it clear for the reader of your code what you're gonna to compare.

Related

C# overloading operator==: Return something else than bool

I'm writing a class library to solve non-linear equations using newton's method. I stumbled across operator-overloading and thought about overloading the ==-Operator. Something like expression1 == expression2 returns the solution as a Constant, which is basically a wrapper-class of System.Double:
public static Constant operator ==(Derivable d1, Derivable d2)
{
return d1.Equal(d2);
}
Although it compiles perfectly fine and works, I was asking myself if it would ever be a reasonable design-choice to overload the ==-Operator to return something else than the equality of two objects as a bool, especially because you also have to overload the !=-Operator. Is this bad practice and should I rather just use my method Equal?
As a fellow developer, I would suggest not overriding the == Operator (C# Reference).
For predefined value types, the equality operator (==) returns true if
the values of its operands are equal, false otherwise. For reference
types other than string, == returns true if its two operands refer to
the same object. For the string type, == compares the values of the
strings.
I can't imagine a scenario where you would want to override this behavior. If you are working with classes then you could override the Object.Equals Method (Object).
If you are working with other developers this could be very confusing.

Is there a way to define implicit conversion operators in C# for specific versions of generic types?

I have a generic class, MyClass<T>, and I want to be able to implicitly convert from some type, e.g. bool, to a specific version of the generic type, e.g. MyClass<string>. It seems I cannot use any of the following:
fails because "Using the generic type 'MyClass<T>' requires '1' type argument(s)":
public static implicit operator MyClass(bool value) {
return new MyClass<string>(value.ToString());
}
fails because "Unbound generic name is not valid in this context" and because "User-defined conversion must convert to or from the enclosing type":
public static implicit operator MyClass<>(bool value) {
return new MyClass<string>(value.ToString());
}
fails because "User-defined conversion must convert to or from the enclosing type":
public static implicit operator MyClass<string>(bool value) {
return new MyClass<string>(value.ToString());
}
fails because "Cannot implicitly convert type 'MyClass<string>' to 'MyClass<T>'":
public static implicit operator MyClass<T>(bool value) {
return new MyClass<string>(value.ToString());
}
Is there any way this can be achieved, or will I just have to live without it (and incur explicit calls to a conversion method everywhere)?
No, you can't do this. The C# specification is clear, your implicit operator must convert either to or from the type in which it's declared. It has to be an exact conversion, and since the declaring type is exactly MyClass<T>, the conversion has to be either to or from that.
See e.g. Can i use a generic implicit or explicit operator? C# and C# Implicit operator with generic.
At the risk of condoning or endorsing an XY Problem, here are a couple of hacky alternatives:
// Break generics by checking the type explicitly. Requires ugly casting
// and intermediate boxing, though it's possible that with some run-time
// use of Expressions, you could cache a delegate that would handle the
// conversion without the boxing. It'd still be ugly though.
class Class1<T>
{
public Class1(T t) { }
public static implicit operator Class1<T>(bool value)
{
if (typeof(T) == typeof(string))
{
return (Class1<T>)(object)(Class1OfString)value;
}
throw new InvalidOperationException("Invalid type T");
}
}
// Subclass the generic, and declare the conversion there. Of course, then
// to use the conversion, you have to reference this type explicitly. Ugly.
class Class1OfString : Class1<string>
{
public Class1OfString(string text) : base(text) { }
public static implicit operator Class1OfString(bool value)
{
return new Class1OfString(value.ToString());
}
}
class A
{
public static void M()
{
// These all compile and execute fine
Class1OfString c1 = true;
Class1<string> c2 = (Class1OfString)true;
Class1<string> c3 = true;
}
}
There are a number of variations on the themes above, but they all will involve circumventing and special casing the type in some way.
It is worth pointing out that, besides the difficulty dealing with the generic vs. specific here, the use of implicit is suspect for other reasons. The documentation states right at the top that one should use implicit only "if the conversion is guaranteed not to cause a loss of data" and implementations "should never throw exceptions". In both cases, this is "so that they can be used safely without the programmer's awareness". In other words, the very nature of implicit is that they get invoked implicitly, without the programmer necessarily even thinking about it. So they must always work, which wouldn't necessarily be the case with some of the examples above (and in one example, you have to use an explicit syntax anyway, so you might as well implement the operator as explicit anyway).
None of these options are ideal. But frankly, neither is the original scenario. It is odd for a generic type to have to deal with concrete types on a specific basis. It calls into question whether the generic type really should be generic in the first place. It is possible that you really should be doing something more like the subclassing example above, only applied further. I.e. use the generic type for whatever base behavior you need, but put all your specializations into a subclass where you know the type parameter T.
I can't offer more advice than that, given the lack of details in the question. But the basic request is shaky enough to suggest that, if only the question had included a broader problem statement and details about what led you to this actual implementation goal, a better and more applicable answer might have been provided.

Implicit C# Conversion For Generic Wrapper Class

I'm writing a generic wrapper class to implement INotifyPropertyChanged for a bunch of properties within another one of my classes. I've been doing some research on the implicit conversion operator, but I'm a bit confused on how to use it within a generic class. Essentially I would like to get the internally wrapped value without needing to explicitly call the internal property. The behavior I am looking for is essentially how the Nullable<T> class/struct works where if the internal value is not null, then it will return the internally wrapped value directly. Example below:
//current behavior
MyWrapperClass<int> wrapped = new MyWrapperClass();
int startCount = wrapped.Data;
//behavior I am looking to implement
int startCount = wrapped
In the second example above wrapped will return it's internally wrapped value instead of type T instead of having to call the inner property. This is how Nullable<T> behaves.
When looking into implicit conversions it appeared that I needed to know the type before hand per this MSDN article: Using Conversion Operators
Do I need to convert on a dynamic type since the type is not known? Example:
public static implicit operator dynamic(MyWrapperClass w)
Or can I perform implicit conversion on type T as seen below? This would prevent me from making the method static, which I noticed is used in all the sample code I've seen involving both implicit and explicit conversion operators. This option seems "wrong" to me, but I could not find much information on the subject here.
public implicit operator T(MyWrapperClass w)
EDIT: This SO Question might cause this to be labeled as a dupe, but the accepted answer is not what I am looking for since they say to use the property which I am already doing.
After some testing it appears that the second option works without issue and still allows itself to be static. I used #AndersForsgren's answer to this question (not accepted answer) to figure this out. Apparently I misunderstood how the implicit operator overload works. The code snippet that corrects this is as follows:
public static implicit operator T(WrapperClass<T> input)
{
return input.Data;
}

C# short null check syntax

I've recently been coding a lot in both Objective C while also working on several C# projects. In this process, I've found that I miss things in both directions.
In particular, when I code in C# I find I miss the short null check syntax of Objective C.
Why do you suppose in C# you can't check an object for null with a syntax like:
if (maybeNullObject) // works in Objective C, but not C# :(
{
...
}
I agree that if (maybeNullObject != null) is a more verbose / clear syntax, but it feels not only tedious to write it out in code all the time but overly verbose. In addition, I believe the if (maybeNullObject) syntax is generally understood (Javascript, Obj C, and I assume others) by most developers.
I throw this out as a question assuming that perhaps there is a specific reason C# disallows the if (maybeNullObject) syntax. I would think however that the compiler could easily convert an object expression such as if (maybeNullObject) automatically (or automagically) to if (maybeNullObject != null).
Great reference to this question is How an idea becomes a C# language feature?.
Edit
The short null check syntax that I am suggesting would only apply to objects. The short null check would not apply to primitives and types like bool?.
Because if statements in C# are strict. They take only boolean values, nothing else, and there are no subsequent levels of "truthiness" (i.e., 0, null, whatever. They are their own animal and no implicit conversion exists for them).
The compiler could "easily convert" almost any expression to a boolean, but that can cause subtle problems (believe me...) and a conscious decision was made to disallow these implicit conversions.
IMO this was a good choice. You are essentially asking for a one-off implicit conversion where the compiler assumes that, if the expression does not return a boolean result, then the programmer must have wanted to perform a null check. Aside from being a very narrow feature, it is purely syntactic sugar and provides little to no appreciable benefit. As Eric Lippert woudl say, every feature has a cost...
You are asking for a feature which adds needless complexity to the language (yes, it is complex because a type may define an implicit conversion to bool. If that is the case, which check is performed?) only to allow you to not type != null once in a while.
EDIT:
Example of how to define an implicit conversion to bool for #Sam (too long for comments).
class Foo
{
public int SomeVar;
public Foo( int i )
{
SomeVar = i;
}
public static implicit operator bool( Foo f )
{
return f.SomeVar != 0;
}
}
static void Main()
{
var f = new Foo(1);
if( f )
{
Console.Write( "It worked!" );
}
}
One potential collision is with a reference object that defines an implicit conversion to bool.
There is no delineation for the compiler between if(myObject) checking for null or checking for true.
The intent its to leave no ambiguity. You may find it tedious but that short hand is responsible for a number of bugs over the years. C# rightly has a type for booleans and out was a conscience decision not to make 0 mean false and any other value true.
You could write an extension method against System.Object, perhaps called IsNull()?
Of course, that's still an extra 8 or 9 characters on top of the code you'd have to write for the extension class. I think most people are happy with the clarity that an explicit null test brings.

I know I'm doing validation wrong. Please persuade me to stop :)

First let me explain how I currently handle validation, say, for an IPv4 address:
public struct IPv4Address {
private string value;
private IPv4Address(string value) {
this.value = value;
}
private static IPv4Address CheckSyntax(string value) {
// If everything's fine...
return new IPv4Address(value);
// If something's wrong with the syntax...
throw new ApplicationException("message");
}
public static implicit operator IPv4Address(string value) {
return CheckSyntax(value);
}
public static implicit operator string(IPv4Address address) {
return address.value;
}
}
I have a bunch of structs like this one.
They often have additional private members to handle things but not methods are exposed publicly.
Here is a sample usage:
IPv4Address a;
IPv4Address b = "1.2.3.4";
a = b;
b = "5.6.7.8";
string address = a;
// c contains "1.2.3.4"
IPv4Address c = address;
// See if it's valid
try {
IPv4Address d = "111.222.333.444";
}
catch (ApplicationException e) {
// Handle the exception...
}
I can feel that there's something very disturbing with this, hence I'm pondering about switching to a static class with methods like IsIPv4Address and so on.
Now, here's what I think is wrong with the approach above:
New team members will have to wrap their heads around this
It might impede integration with 3rd party code
Exceptions are expensive
Never seen anything like this and I am a conservative type at heart :)
And then what I like about it:
Very close to having a lot of specialized primitives since you have value types.
In practice they can be often used like primitive types would, for example it isn't a problem passing the struct above to a method that accepts a string.
And, my favourite, you can pass these structs between objects and be sure that they contain a syntactically valid value. This also avoids having to always check for correctness, which can be expensive if done unnecessarily and even forgotten.
I can't find a fatal flaw to the approach above (just a beginner here), what do you think?
Edit: as you can infer from the first line this is only an example, I'm not asking for a way to validate an IP address.
First of all, you should read posts about implicit casting, and when to use it (and why it's bad to use it in your scenario), you can start here.
If you need to have checking methods, they should be public static, instead of such strange constructs, beside this, having such methods allows you to choose if you want to throw exceptions (like .Parse() methods do), or signal by returning some value that should be checked (like .TryParse() methods).
Beside this, having static methods for creating valid objects doesn't mean you can't use value type (struct) instead of a class, if you really want to. Also, remember that structs have implicit empty constructor, that you can't "hide", so even your construct can be used like this:
IPv4Address a = new IPv4Address();
which will give you invalid struct (value will be null).
I also very much like the concept of mimicking the primitive patterns in the framework, so I would follow that through and lean toward IpV4Address.Parse("1.2.3.4") (along with TryParse) rather than implicit cast.
Seems like a whole lot of complexity with no benefit. Just because you can doesn't mean you should.
Instead of a CheckSyntax(string value) that returns a IP (This method is poorly worded btw) I would just have something like a
bool IsIP(string)
Then you could put this in a utility class, or a base class, or a separate abstraction someplace.
From the MSDN topic on implicit conversions
The pre-defined implicit conversions
always succeed and never cause
exceptions to be thrown. Properly
designed user-defined implicit
conversions should exhibit these
characteristics as well
Microsoft itself has not always followed this recommendation. The following use of the implicit operator of System.Xml.Linq.XName throws an XmlException:
XName xname = ":";
Maybe the designers of System.Xml.Linq got away with it by not documenting the exception :-)

Categories