C # List <MyObj> Delete Method

In C #, I have:

public class MyObj
{
 // methods and properties etc.
} 

List<MyObj> listOfObjects = new List<MyObj>();

listOfObjects.Remove( certainObj ); 

I want to know how C # knows that someObj is in a list ... Does Equals () use it to find a specific class in a list?

+3
source share
3 answers

Yes, it uses a method Equals(...) MyObjthat, by default, refers to equality of links if the method has not been redefined.

In particular, it uses EqualityComparer<T>.Default, which, in turn, looks if it MyObjimplements IEquatable<T>. If not, it will use an overload myObj.Equals(object obj)that causes Object.ReferenceEquals(...), if not overridden.

+4
source

See http://msdn.microsoft.com/en-us/library/cd666k3e.aspx

, EqualityComparer.Default T, .

; O (n) n - .

0

Yes, check the MSDN for the List class, which uses the .Equals () method for the for object. Contains in the general list.

http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

0
source

Source: https://habr.com/ru/post/1780073/


All Articles