Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9102771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:27:40+00:00 2026-06-17T01:27:40+00:00

I have a 2 classes that contain data that will populate separate grids. The

  • 0

I have a 2 classes that contain data that will populate separate grids. The grids are very similar, but different enough to require the use of 2 classes. Both grids contain a function called “GetDuplicates” and where I am implementing these classes, I have a method that checks if the class has duplicates and returns a message indicating so.

private bool HasDuplicates(FirstGridList firstList)
{
    var duplicates = firstList.FindDuplicates();
    if (duplicates.Count > 0)
    {
        // Do Something
        return true;
    }
    return false;
}

I would like to be able to call that method using both FirstGridList and SecondGridList. I just don’t know how to go about properly implementing the generic constraint then casting the generic input parameter to the correct type. Similar to:

private bool HasDuplicates<T>(T gridList)
{
    // Somehow cast the gridList to the specific type
    // either FirstGridList or SecondGridList

    // Both FirstGridList and SecondGridList have a method FindDuplicates
    // that both return a List<string>
    var duplicates = gridList.FindDuplicates();
    if (duplicates.Count > 0)
    {
        // Do Something
        return true;
    }
    return false;
}

As you can see, the method does the same thing. Therefore I do not want to create this twice. I feel like this is possible, but I am thinking about it incorrectly. I am not exactly experienced with generics yet. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T01:27:41+00:00Added an answer on June 17, 2026 at 1:27 am

    You could have both your grids implement a common interface like:

    public interface IGridList
    {
        public IList<string> FindDuplicates();
    }
    

    and then define your generic constraint based on this interface:

    private bool HasDuplicates<T>(T gridList) where T: IGridList
    {
        // Both FirstGridList and SecondGridList have a method FindDuplicates
        // that both return a List<string>
        var duplicates = gridList.FindDuplicates();
        if (duplicates.Count > 0)
        {
            // Do Something
            return true;
        }
        return false;
    }
    

    Obviously both your FirstGridList and SecondGridList should implement the IGridList interface and the FindDuplicates method.

    or you could even get rid of the generics at this stage:

    private bool HasDuplicates(IGridList gridList)
    {
        // Both FirstGridList and SecondGridList have a method FindDuplicates
        // that both return a List<string>
        var duplicates = gridList.FindDuplicates();
        if (duplicates.Count > 0)
        {
            // Do Something
            return true;
        }
        return false;
    }
    

    By the way at this stage you could even get rid of the HasDuplicates method as it doesn’t bring much value to your application. I mean Object Oriented Programming existed long before things like generics or LINQ so why not use it:

    IGridList gridList = ... get whatever implementation you like
    bool hasDuplicates = gridList.FindDuplicates().Count > 0;
    

    Seems reasonable and readable enough to any developer with basic C# culture. And of course it spares you a couple of lines of code. And remember that the more code you write the higher the probability to make mistakes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classes that both contain a static variable, XmlTag. The second
I have a number of static classes that contain tables like this: using System;
I have classes that are named exactly the same across different plug-ins that I
Say I have two classes with different names but the exactly same structure. It
I have a master-detail relationship between 2 classes. The master class will contain a
I have an ASP.NET project that contains many classes. I am thinking about creating
I have a class that contains objects of two other classes. I need one
I have a web application that contains a bunch of classes in the App_Code
Consider a situation. I have an in-proc COM server that contains two COM classes.
I have an MFC extension DLL that I've written. It contains derived classes of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.