xamarincomparisonxamarin.formsimagesource

Xamarin.Forms: Comparing ImageSource


If cachePath would be a string I could do this

if(value.Equals(this.cachePath, StringComparison.Ordinal))

Now cachePath is of type ImageSource. How can I compare if value is the same as cachePath? Currently I'm doing this

if(this.cachePath == value)

Is this the only way? Or how can Equals() be used for this?


Solution

  • So this comes down to the age old "Pass by reference" / "Pass by Value" discussion.

    Because ImageSource is a Reference Type comparing two ImageSources will compare the References. NOT the actual data.

    because String is a Value Type the comparison WILL compare the actual string and not the reference.

    For a true comparison of your ImageSource want to convert to a Byte[] and compare the two byte arrays as these will be Value types

    With reference to your question:

    If this.cachePath is the same object as value. i.e:

    this.cachePath = value
    this.cachePath == value
    //Returns true
    

    BUT if cachePath is a copy of the image value then EVENTHO the images ARE the same, because their references are not pointing to the same object

    this.cachePath = new ImageSource();
    this.value = new ImageSource(cachePath.Source);
    this.cachePath == Value 
    // Returns False