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

  • SEARCH
  • Home
  • 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 7078603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:34:06+00:00 2026-05-28T06:34:06+00:00

I have defined an abstract method in my base class to take in a

  • 0

I have defined an abstract method in my base class to take in a Generic type. What I want to be able to do is pass in a list of one of my sub classes to this abstract method. I am just not sure how to properly handle this in the sub class?

Base Class Abstract Method:

protected abstract void Validate<T>(T records);

SubClass Implementation (Where I am having issues):

Call Method (Pass in List of LogEventRecord):

Validate<List<LogEventRecord>>(records);

Method (Want to handle List of LogEventRecord):

      protected override void Validate<T>(T records)
        {
          //How do I handle records here? I want them to be List<LogEventRecord> and when i debug
          //They appear to be coming in that way, but I can't utilize them in the method
          //How do I cast the records to List<LogEventRecord>? Is that what I do?

        }

Any suggestions would be greatly appreciated.

  • 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-05-28T06:34:07+00:00Added an answer on May 28, 2026 at 6:34 am

    Declare the method like this:

    protected abstract void Validate<T>(IEnumerable<T> records)
    

    And call it like this:

    Validate(records); 
    

    The compiler should be able to infer the type for generic methods, no need to include it explicitly. If you really want to include the type or if type inference fails for some reason (you’ll know this because the compiler will complain at you), you can do this:

    Validate<LogEventRecord>(records);
    

    Then your implementations can use the records like this:

    protected override void Validate<T>(IEnumerable<T> records)
    {
        //you can use a foreach loop
        foreach (T record in records)
        {
    
        }
    
        //Or "linq" operators:
        bool isValid = records.Any(r => r.IsNotValid);
    
        //Or linq query comprehension syntax:
        bool isValid2 = (from r in records 
                      where r.IsNotValid
                      select r).Any();
    
    }
    

    But right now the only thing you know about T is that it’s an object. You can’t do much with variables of type “object”, and so this isn’t very useful yet. In fact, the latter two options in my sample will fail right now because the .IsNotValid property probably doesn’t exist.

    Instead, you probably want (maybe already have) an interface describing the objects you will use with this function: they’re probably always going to be either logs or records of some common base type. If that’s the case, you have a two options. The first is to constrain your generic type. You do that by changing your method signature like this:

    protected abstract void Validate<T>(IEnumerable<T> records) where T : MyRecordInterface
    

    The other option is that in C# 4 there is new support for variance on interfaces (including IEnumerabe<T> that would allow you to avoid the need for a generic method here at all. To take advantage of this, just make sure you’re using .Net 4 in Visual Studio 2010 or later and declare the method like this:

    protected abstract void Validate(IEnumerable<MyRecordInterface> records)
    

    You need .Net 4, and not just c# 4 targeting an earlier version of the framework, because you need the .Net 4 version of the IEnumerable<T> interface which is built to include the necessary support for variance.

    Finally, there is a third option. You could add a delegate parameter to your method to convert each item in the list to a boolean, like so:

    protected abstract void Validate<T>(IEnumerable<T> records, Func<T, bool> isValid)
    

    .

    protected override void Validate<T>(IEnumerable<T> records, Func<T, bool> isValid)
    {
        bool recordsAreValid = records.Any(r => !isValid(r));
    }
    

    .

    Validate(records, l => ((LogEventRecord)l).IsValid);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static method defined in a base class, I want to override
I have the following base abstract class defined as: public abstract class BaseObject<T> :
Design -- in a perfect world I have one abstract base class A with
I have an abstract base class and I want to declare a field or
I have an abstract base class from which many classes are derived. I want
Let's say I have defined the following class: public abstract class Event { public
I have defined a custom list template type for SharePoint . I install it
I have an abstract immutable base class that defines enforces child classes to be
I have an abstract base class which acts as an interface. I have two
Here's the situation. I have a base class which implements a method that accepts

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.