Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
Method for class list object

I know this is a bit odd and I do know another way to work around this but I was wonder if it is possible to have a method that would affect a list of itself. I'll explain.

This would be my workaround

public class Example
{
    public void Sanitize()
    {
        // Does logic sanitize itself using API
    }

    public static List<Example> Sanitize(List<Example> examples)
    {
        /// returns a list of sanitized Examples
    }
}

How the Example would be able to work on its own.

// Base Example sanitized
Example example = new Example(...);
example.Sanitize();

What I would also like to do

// Whole list sanitized
List<Example> examples = new List<Example> {...};
examples.Sanitize();

Is there a way to do that instead of being required to do this?

List<Example> startingExamples = new List<Example> { ... }
List<Example> endingExamples = Example.Sanitize(startingExamples);
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can have your static method iterate and mutate the list elements in place.

public class Example
{
    public void Sanitize()
    {
        // Does logic sanitize itself using API
    }

    public static void Sanitize(List<Example> examples)
    {
        foreach (var example in examples)
        {
            example.Sanitize();
        }
    }
}

Note that you cannot modify the list itself while iterating it, but you can make changes to the elements of the list.

over 4 years ago · Santiago Trujillo Report

0

You can use an extension method to add functionality to the list.

static class ExtensionMethods
{
    public static void Sanitize(this List<Example> source)
    {
        foreach (var item in source) item.Sanitize();
    }
}

Now you can do this:

var list = new List<Example>();
list.Sanitize();
over 4 years ago · Santiago Trujillo Report

0

Looks like you could use an extension method.

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type. For client code written in C#, F# and Visual Basic, there's no apparent difference between calling an extension method and the methods defined in a type.

An extension method is a static method on a static class that is visible to the code that is using it. For example, public code. The first parameter of the method is the type that the method operates on and must be preceded with the this modifier.

So, for example, you could do something like this...

public static class EnumerableOfExampleExtensions
{
    public static void Sanitize(this IEnumerable<Example> examples) /*or List is fine*/
    {
        // Null checks on examples...
        foreach (var example in examples)
        {
            example.Sanitize();
        }
    }
}

Then you can call it as an instance method on any IEnumerable<Example>.

List<Example> examples = new List<Example>();
examples.Sanitize();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!