I have a custom class called CustomClass. It contains a variable called "Name" and a list of values (for the sake of simplicity let's make this an int - in reality it is another custom class, but the principle should be the same).
So :
public class CustomClass {
string name;
}
I have a List<CustomClass>.
When I attempt to add a value to this List, the logic I want, is for this List to check if it contains a CustomClass with the same name as the value I want to add.
If it does, then do x, otherwise, do y.
listOfCustomClass.Contains(customClassToAdd.name) will not work in this case, I assume, however this is the functionality I require.
What is best practice here ?
I think you can try something like var x = MyList.Where(C=> C.Name == InsertedName) and check the result (not tested)
You'll have to create a new class,let's call it CustomList, that inherits from IList<> where you can override the add method, do your check, and then add it to the base. Something like this:
public class CustomList<T> : IList<T> where T : CustomClass
{
private List<T> innerlist;
public void Add(T item)
{
if(innerlist.Any(a => a.Name == item.Name)))
innerlist.Add(item);
}
}
you can do it using linq as follow but you have to make name field public.
List<CustomClass> list = new List<CustomClass>();
CustomClass toCheck = new CustomClass();
if (list.Any(p => p.name.Equals(toCheck)))
{
//do x here
}
else
{
//do y here
}
however if you don't want to use linq then Do some changes in CustomClass as follow
public class CustomClass
{
string name;
List<int> intLost = new List<int>();
public override bool Equals(object obj)
{
return this.Equals(obj as CustomClass);
}
public override int GetHashCode()
{
return 0;
}
public bool Equals(CustomClass cc)
{
if (cc == null) return false;
return this.name.Equals(cc.name);
}
}
Then you can do this.
List<CustomClass> list = new List<CustomClass>();
CustomClass toCheck = new CustomClass();
if (list.Contains(toCheck))
{
//do x here
}
else
{
//do y here
}
It seems to me that you want to override the .Add() behavior of your List<CustomClass>. While you could use extension methods, I think a better solution would be to invent a class that extends List in some manner. I'd recommend implementing IList in your collection class if you need to have that level of control over add operations...
public class CustomClassList : IList<CustomClass>
{
public void Add (CustomClass item)
{
if(this.Select(t => t.Name).Contains(item.Name))
// Do whatever here...
else
// Do whatever else here...
}
// ... other IList implementations here ...
}
try this:
IList<CustomClass> list = new List<CustomClass>();
CustomClass customClass = new CustomClass();
customClass.name = "Lucas";
if((list.Tolist().Find(x => x.name == customClass.name)) == null)
{
list.Add(customClass);
}
else
{
//do y;
}
You could override the Equals(object o) function in your CustomClass, so that two CustomClasses are considered equal if their names are the same. Then
listOfCustomClass.Contains(customClassToAdd);
should work.
Another way is to override Equals method on your CustomClass and then just call List.Contains()
If the name property uniquely identifies the CustomClass, then you should overload Equals and GetHashCode(). The reason List.Contains doesn't work is that underneath the HashCodes are compared. So you need to overload GetHashCode and Equals something like this:
public override int GetHashCode()
{
return this.name.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as CustomClass;
if (other != null)
{
if (other.Name == this.Name)
{
return true;
}
}
return false;
}
Related
I am doing unit testing, and basically want to check that the data that 2 objects hold is the same
Assert.AreEqual(object1, object2);
Assert.IsTrue(object1.Equals(object2)); //this of course doesn't work
I am searching for the C# equivalent of assertJ
Assert.That(object1).isEqualToComparingFieldByField(object2)
You could either use records (c# 9 +) or you have to override the Equals method (if you have access and you can change the objects that you're working with).
Records example:
var point = new Point(3, 4);
var point2 = new Point(3, 4);
var test = point.Equals(point2); //this is true
public record Point(int X, int Y);
with classes:
public class Point
{
public int X { get; }
public int Y { get; }
public override bool Equals(object? obj)
{
if (obj == null)
return false;
return obj is Point point && (point.X == X && point.Y == Y);
}
public override int GetHashCode()
{
return HashCode.Combine(X, Y);
}
}
if you are not allowed to touch the implementation, then you could use serialization and compare the strings:
var obj1Str = JsonConvert.SerializeObject(object1);
var obj2Str = JsonConvert.SerializeObject(object2);
Assert.Equal(obj1Str, obj2Str);
using Newtonsoft.Json nuget
C# classes are reference equality, which means that variables are the same using the standard Equals and == if they point to the same object, you could override that behaivour, but it may break something now or in the future.
Or, you could switch to using a construct that's value equality by default, which structs as well as record classes are. If you can't (or don't want to) do that you can implement a value equals "helper" method yourself. I would not recommend overriding the Equals method or the == operator, as that can (and most likely will) lead to errors in the future instead I recommend you write your own ValueEquals method or extension method, something along the lines of
class Foo
{
public int Count {get; set;}
public string Message {get; set;}
}
public static bool ValueEquals(this Foo self, Foo other)
{
return self.Count == other.Count && self.Message == other.Message;
}
public void MyTest()
{
// Arrange and Act
...
// Assert
Assert.IsTrue(myFoo1.ValueEquals(myFoo2));
}
Depending on whether or not you can/ want to add a ValueEquals to your Foo class you can decide on doing it with an extension method or a normal method.
You could also implement a IEqualityComparer<T> like
public class FooValueEqualityComparer : IEqualityComparer<Foo>
{
public bool Equals(Foo foo1, Foo foo2)
{
return foo1.Count == foo2.Count &&
foo1.Message == foo2.Message;
}
public int GetHashCode(Foo foo)
{
return foo.GetHashCode();
}
}
// Use it
public void MyTest()
{
// Arrange and Act
...
// Assert
Assert.IsTrue(new FooEqualityComparer().Equals(myFoo1, myFoo2));
}
Or, you could write a generic ValueEquals that works for all^* classes using Reflection:
public static class ValueEqualityComparer
{
public static bool ValueEquals<T>(this T self, T other) where T : class
{
var type = self.GetType();
if (type == typeof(string))
return self.Equals(other);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var selfValue = property.GetValue(self);
var otherValue = property.GetValue(other);
// String is special, it's not primitive but is value equality like primitives
if (property.PropertyType.IsPrimitive || property.PropertyType == typeof(string))
{
if (!selfValue.Equals(otherValue))
return false;
}
// If the property is a List value equals each member
// Maybe find another type that allows indexing and is less restrictive
else if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
var selfList = ((IEnumerable)property.GetValue(self)).Cast<object>();
var otherList = ((IEnumerable)property.GetValue(other)).Cast<object>();
try
{
// Using EquiZip from MoreLinq: https://github.com/morelinq/MoreLINQ/blob/master/MoreLinq/EquiZip.cs
foreach (var element in selfList.EquiZip(otherList, (selfItem, otherItem) => new { selfItem, otherItem }))
{
if (!ValueEquals(element.selfItem, element.otherItem))
return false;
}
}
catch (InvalidOperationException)
{
// MoreLINQ throws a InvalidOperationException if our two enumerables aren't the same length
return false;
}
}
else
{
if (!ValueEquals(selfValue, otherValue))
return false;
}
}
return true;
}
}
This implementation is by no means perfect, and should honestly only be used for UnitTests and also should be thoroughly tested itself. You can see my tests as a dotnetfiddle here
Or you could do it "dirty" and serialize the objects to a string and compare those values.
filterCollection is a list of int's
I am trying to create a new list from an existing with distinct with Linq
In this cause which is better
filterCollection.Select(filterId => new FilterTable() { FilterId = filtertId }).Distinct().ToList();
OR
filterCollection.Distinct().Select(filterId => new FilterTable() { FilterId = filtertId }).ToList();
I am not sure this is correct.
It depends. It depends on what is in filterCollection, if its Linq-To-Objects or Linq-To-Entities (database driven Linq provider) It depends on the implementation of FilterTable, because if this class does not override Equals and GetHashCode it will not work at all.
Since you project filterId i assume that the it's IEnumerable<int>(or string), then Distinct before the Select will work, it will remove duplicates. But after the Select it might not work because you create new FilterTable instances.
To make it work implement IEquatable<FilterTable> and override Equals+GetHashCode:
public class FilterTable : IEquatable<FilterTable>
{
public int FilterId { get;set; }
public bool Equals(FilterTable other)
{
return FilterId == other?.FilterId;
}
public override bool Equals(object obj)
{
return obj is FilterTable ft && this.Equals(ft);
}
public override int GetHashCode()
{
return FilterId.GetHashCode();
}
}
I created class with overriden Equals. The problem is that Distinct method doesn't work for my class.
class MyClass
{
public int Item1 { get; private set; }
public int Item2 { get; private set; }
public MyClass(int item1, int item2)=>(Item1,Item2)=(item1,item2);
public override bool Equals(object obj)
{
var other = obj as MyClass;
if (other == null)
{
return false;
}
return (this.Item1 == other.Item1 && this.Item2 == other.Item2);
}
}
class Program
{
static void Main(string[] args)
{
MyClass x = new MyClass(1, 0);
MyClass y = new MyClass(1, 0);
var list = new List<MyClass>();
list.Add(x);
list.Add(y);
bool b = x.Equals(y)); //True
var distincts = list.Distinct(); //Doesn't work, contains both
}
}
How can I fix that and why it doesn't use my Equals in Distinct?
Distinct docs:
Returns distinct elements from a sequence by using the default equality comparer to compare values.
Let's see what the default equality comparer does:
The Default property checks whether type T implements the System.IEquatable<T> interface and, if so, returns an EqualityComparer<T> that uses that implementation. Otherwise, it returns an EqualityComparer<T> that uses the overrides of Object.Equals and Object.GetHashCode provided by T.
So basically, to make this work, you either:
implement GetHashCode as well
implement IEquatable<T>
Call the overload of Distinct that accepts a custom equality comparer.
If I were you, I would choose the second one because you need to change the least of your code.
class MyClass: IEquatable<MyClass> {
...
public bool Equals(MyClass obj)
{
if (obj == null)
{
return false;
}
return (this.Item1 == obj.Item1 && this.Item2 == obj.Item2);
}
}
You have to override GetHashCode as well:
public override int GetHashCode()
{
return Item1; // or something
}
Distinct first compares the hashcodes, which should be computed faster than the actual Equals. Equals is only further evaulated if the hashcodes are equal for two instances.
You need to implement IEquatable<MyClass> in MyClass and provide your own implementation of GetHashCode and Equals method.
see this for more information.
class MyClass
{
public int Item1 { get; private set; }
public int Item2 { get; private set; }
public MyClass(int item1, int item2)=>(Item1,Item2)=(item1,item2);
public override bool Equals(object obj)
{
var other = obj as MyClass;
if (other == null)
{
return false;
}
return (this.Item1 == other.Item1 && this.Item2 == other.Item2);
}
public override int GetHashCode()
{
return this.Item1;
}
}
I have a custom list class let say,
public class Fruit
{
public string Name { get; set; }
public string Size { get; set; }
public string Weight{ get; set; }
}
Now I am adding records to it like this,
List<Fruit> Fruits= new List<Fruit>();
//some foreach loop
Fruit fruit = new Fruit();
fruit.Name = ...;
fruit.Size = ...;
fruit.Weight = ...;
Fruits.Add(fruit);
What I want ?
I want to make changes to Public Fruit Class in a way that it checks if any of fruit in custom list has already has same weight then just ignore it and continue e.g. don't add it to the list.
I would prefer doing it without changing foreach loop logic
Use LINQ .Any() - Determines whether any element of a sequence exists or satisfies a condition. (MSDN: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.any.aspx)
if (!Fruits.Any(f => fruit.Weight != null && f.Weight == fruit.Weight))
Fruits.Add(fruit);
If duplicate weights are not allowed i would use a HashSet<Fruit> with a custom IEqualityComparer:
public class FruitWeightComparer : IEqualityComparer<Fruit>
{
public bool Equals(Fruit x, Fruit y)
{
if(x == null || y== null) return false;
if (Object.ReferenceEquals(x, y)) return true;
return x.Weight == y.Weight;
}
public int GetHashCode(Fruit obj)
{
return obj.Weight == null ? 0 : obj.Weight.GetHashCode();
}
}
Now you can use the HashSet constructor with this comparer:
HashSet<Fruit> Fruits = new HashSet<Fruit>(new FruitWeightComparer());
// ...
bool notInSet = Fruits.Add(fruit);
HashSet.Add returns true if the item could be added.
You can control it at insert time by simply not inserting already existing fruit
if (!myFruits.Any(f => f.Weight == newFruit.Weight))
myFruits.Add(newFruit);
If you can't manipulate the insertion logic you can make a custom list that wraps a normal List<T> and changes the behavior of Add like in the above example:
public class FruitsWithDistinctWeightList : IEnumerable<Fruit>
{
private List<Fruit> internalList;
... // Constructor etc.
public void Add(Fruit fruit)
{
if (!internalList.Any(f => f.Weight == fruit.Weight))
internalList.Add(fruit);
}
... // Further impl of IEnumerable<Fruit> or IList<Fruit>
}
You could also use some existing collection that does not allow duplicate items. For example some hash based collection such as HashSet<Fruit>:
var fruitsWithDistinctWeight = new HashSet<Fruit>(new FruitWeightComparer());
Where you'd use a comparer that says fruits with equal weight are equal:
public class FruitWeightComparer : IEqualityComparer<Fruit>
{
public bool Equals(Fruit one, Fruit two)
{
return one.Weight == two.Weight;
}
public int GetHashCode(Fruit item)
{
return one.Weight.GetHashCode();
}
}
Note that a HashSet<T> is not ordered like a list is. Note that all of the code above for simplicity assumes that the Weight field is set. If you have public setters on your class (i.e. no guarantees of this) you would have to change appropriately.
I am using a System.Collections.Generic, which contains instances of a class I wrote.
I have read that the collections .Contains method uses object.Equals(), or an implementation of the Equals() method from the IEquatable interface.
I have overridden the object method, as well as implemented from the interface. However, Queue.Contains(instance) is always returning false. What am I doing wrong?
For instance...
class Foo : IEquatable<Foo>
{
...
int fooField1;
...
public override bool Equals(object obj)
{
Foo other = obj as Foo;
bool isEqual = false;
if (other.fooField1 == this.fooField1)
{
isEqual = true;
}
return isEqual;
}
public bool Equals(Foo other)
{
bool isEqual = false;
if (other.fooField1 == this.fooField1)
{
isEqual = true;
}
return isEqual;
}
}
...
void SomeFunction()
{
Queue<Foo> Q = new Queue<Foo>();
Foo fooInstance1 = new Foo();
Foo fooInstance2 = new Foo();
fooInstance1.fooField1 = 5;
fooInstance2.fooField1 = 5;
Q.Enqueue(fooInstanec1);
if(Q.Contains(fooInstance2) == false)
{
Q.Enqueue(fooInstance2);
}
}
fooInstance2 is always added to the queue. In fact, when I run this on the debugger, the implementations of Equals are never reached.
What am I doing wrong?
Your sample code works as expected once the initial compile errors are sorted out. Not that it is relevant to the posed problem, Do read up on overriding Equals (you need to override GetHashCode too and check for error cases like null / type-mismatch).
class Foo : IEquatable<Foo>
{
private int _fooField1;
public Foo(int value)
{
_fooField1 = value;
}
public override bool Equals(object obj)
{
return Equals(obj as Foo);
}
public bool Equals(Foo other)
{
return (other._fooField1 == this._fooField1);
}
}
class Program
{
static void SomeFunction()
{
var Q = new Queue<Foo>();
Foo fooInstance1 = new Foo(5);
Foo fooInstance2 = new Foo(5);
Q.Enqueue(fooInstance1);
if (!Q.Contains(fooInstance2))
{
Q.Enqueue(fooInstance2);
}
else
{
Console.Out.WriteLine("Q already contains an equivalent instance ");
}
}
static void Main(string[] args)
{
SomeFunction();
}
}
You need GetHashCode() method overridden as well in your class Foo.
fooInstance1.fooField1 = 5;
fooInstance1.fooField2 = 5;
You updated fooInstance1 twice there. Second line should say fooInstance2.fooField1 = 5;.
Once that's fixed, Q.Contains returns True as expected.
Other than that, you don't necessarily need to implement IEquatable. Every object has an overridable Equals method. You can simply overwrite that. Be careful when you implement your own comparison method. Your implementation shown in this sample is very open to NullReference exceptions. Something like this would be better:
public override bool Equals(object obj)
{
if(obj == null)
return false;
Foo other = obj as Foo;
if(other == null)
return false;
return fooField1 == other.fooField1;
}
As others have mentioned, if you go this route and override Equals, you should override GetHashCode, too. There are a few other things you should consider. See this MSDN page for details.
Why is it important to override GetHashCode when Equals method is overriden in C#?