C# and implicit conversions

Sometime you decide to create a small datastructure – let’s call it Trinary ;) – which will mostly behave like some standard datastructure – let’s think of Boolean – but with some different behavior from time to time. TO avoid a lot of noisy changes to your codebase it might be better to define an implicit conversation from your new datastructure to the standard datastructure (and vice versa). Fortunately this is very easy within C# as all you have to do is to implement appropriate “public static implicit operator $DestType($SourceType myParam)” methods for the conversion:

public static implicit operator Trinary(Boolean myBoolean)
{
  if (myBoolean) return Trinary.TRUE;
  return Trinary.FALSE;
}

public static implicit operator Boolean(Trinary myTrinary)
{
  if (myTrinary == Trinary.FALSE) return false;
  if (myTrinary == Trinary.DELETED) return false;
  return true;
}
 

The complete class might look like the following:

public sealed class Trinary
{

#region Data

public readonly String Name;
public readonly Byte Value;

#endregion

#region Init default values

public static readonly Trinary FALSE   = new Trinary(0, "FALSE");
public static readonly Trinary TRUE    = new Trinary(1, "TRUE");
public static readonly Trinary DELETED = new Trinary(2, "DELETED");

#endregion

#region (private) Constructor

private Trinary(Byte myValue, String myName)
{
this.Name   = myName;
this.Value  = myValue;
}

#endregion

#region Implicit conversion to/from Boolean

public static implicit operator Trinary(Boolean myBoolean)
{

if (myBoolean)
return Trinary.TRUE;

return Trinary.FALSE;

}

public static implicit operator Boolean(Trinary myTrinary)
{

if (myTrinary == Trinary.FALSE)
return false;

if (myTrinary == Trinary.DELETED)
return false;

//if (myTrinary == Trinary.TRUE)
return true;

}

#endregion

public int CompareTo(Boolean myBoolean)
{

if (Value == 0 && myBoolean == false)
return 0;

if (Value == 2 && myBoolean == false)
return 0;

if (Value == 1 && myBoolean == true)
return 0;

return 1;

}

public int CompareTo(Trinary myTrinary)
{

if (this.Equals(myTrinary))
return 0;

return 1;

}

#region Operator overloading

#region Equals(myObject)

public override Boolean Equals(Object myObject)
{

// Check if myObject is null
if (myObject == null)
return false;

// If parameter cannot be cast to Point return false.
Trinary _Trinary = myObject as Trinary;
if ((Object)_Trinary == null)
return false;

return Equals(_Trinary);

}

#endregion

#region Equals(myRevisionID)

public Boolean Equals(Trinary myTrinary)
{

// If parameter is null return false:
if ((Object)myTrinary == null)
return false;

// Check if the inner fields have the same values
if (Value != myTrinary.Value)
return false;

if (Name != myTrinary.Name)
return false;

return true;

}

#endregion

#region Operator == (myTrinary1, myTrinary2)

public static Boolean operator == (Trinary myTrinary1, Trinary myTrinary2)
{

// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(myTrinary1, myTrinary2))
return true;

// If one is null, but not both, return false.
if (((Object)myTrinary1 == null) || ((Object)myTrinary2 == null))
return false;

return myTrinary1.Equals(myTrinary2);

}

#endregion

#region Operator != (myTrinary1, myTrinary2)

public static Boolean operator !=(Trinary myTrinary1, Trinary myTrinary2)
{
return !(myTrinary1 == myTrinary2);
}

#endregion

#region GetHashCode()

public override int GetHashCode()
{
return Value.GetHashCode() ^ Name.GetHashCode();
}

#endregion

#endregion

#region ToString

public override String ToString()
{
return Name;
}

#endregion

}
 

Additionally it might be usefull to define an extension method to compare the standard Boolean to your new datastructure:

public static class TrinaryExtensionMethods
{

public static int CompareTo(this Boolean myBoolean, Trinary myTrinary)
{

if (myTrinary == Trinary.FALSE    && myBoolean == false)
return 0;

if (myTrinary == Trinary.DELETED  && myBoolean == false)
return 0;

if (myTrinary == Trinary.TRUE     && myBoolean == true)
return 0;

return 1;

}

}
 

Recent Entries

2 Responses to “C# and implicit conversions”

  1. dalini Says:

    ok zum x-ten:
    true, false und delete – interessante combi ;)

    ps: dein anti-spam image sucks (funzt net gut)

    [Reply]

  2. XlJoan Says:

    Whether we cherish it or not, quondam in our lives, we would require to carry out term papers and other written activity. It is beyond any doubt not clear but very curious to make a analyze about this good topic. To do that, you need to read a lot of articles and books, or you can just order some kind of work and then use plagiarism checker done by http://www.plagiarismsearch.com and save your time like some quick help with the American Dream.

    [Reply]

Leave a Reply

Enter this code