none
Software pattern - interface, where RRS feed

  • Question

  • Hello!

    When does it make sense to add the Where condition to classes?
    Are there examples and simple explanations? Thanks in advance.

    I found this on google.

    public class GenericCompare<T> : IEqualityComparer<T> where T : class
    {
        private Func<T, object> _expr { get; set; }
        public GenericCompare(Func<T, object> expr)
        {
            this._expr = expr;
        }
        public bool Equals(T x, T y)
        {
            var first = _expr.Invoke(x);
            var sec = _expr.Invoke(y);
            if (first != null && first.Equals(sec))
                return true;
            else
                return false;
        }
        public int GetHashCode(T obj)
        {
            return obj.GetHashCode();
        }
    }
    
    
    Greetings Markus

    Tuesday, February 25, 2020 5:31 PM

Answers

  • Hello,

    The code sample you posted from Stackoverflow does not make sense. What makes sense is to use a targeted comparer to a specific class which also will be better performance wise then a generic version.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Markus Freitag Wednesday, February 26, 2020 5:24 PM
    Tuesday, February 25, 2020 6:13 PM
    Moderator
  • Hi Markus,

    Thank you for posting here.

    This is a constraint on the parameters of the generic class.

    For example, if "where T" is followed by "class", it means that the parameters of this generic class can only be reference types. If you use int, an error occurs.

    This is described in detail in Microsoft's documentation.

    Constraints on type parameters (C# Programming Guide)

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, February 26, 2020 3:20 AM

All replies

  • Hello,

    The code sample you posted from Stackoverflow does not make sense. What makes sense is to use a targeted comparer to a specific class which also will be better performance wise then a generic version.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Markus Freitag Wednesday, February 26, 2020 5:24 PM
    Tuesday, February 25, 2020 6:13 PM
    Moderator
  • You do know that if a method is using a collection and returning the collection from the method, you can use Linq on the method. 

    var results = SomeMethod().where(a => a.Name == "Marcus").FirstOrDefault();

    You can probably do it with a dictionary too.

    Tuesday, February 25, 2020 6:46 PM
  • Hi Markus,

    Thank you for posting here.

    This is a constraint on the parameters of the generic class.

    For example, if "where T" is followed by "class", it means that the parameters of this generic class can only be reference types. If you use int, an error occurs.

    This is described in detail in Microsoft's documentation.

    Constraints on type parameters (C# Programming Guide)

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, February 26, 2020 3:20 AM