Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override functions from String class in C#

For example, I need to see if a string contains a substring, so I just do:

String helloworld = "Hello World";
if(helloworld.Contains("ello"){
    //do something
}

but if I have an array of items

String helloworld = "Hello World";
String items = { "He", "el", "lo" };

I needed to create a function inside the String class that would return true if either of the items inside the array is contained in the string, for example.

I would like to override the function Contains(string) with Contains(IEnumerable) for this scenario, instead of creating a function in another class. Is it possible to do this, and if so, how can we override the function? Thank you very much.

So here goes the complete solution (thanks guys):

public static bool ContainsAny(this string thisString, params string[] str) {
    return str.Any(a => thisString.Contains(a));
}
like image 938
Jronny Avatar asked Jun 29 '10 01:06

Jronny


People also ask

How do you override a function?

Overriding occurs in a parent class and its child class. No special keyword is used to overload a function. Virtual keyword in the base class and Override keyword in the derived class can be used to override a function.

Can you override methods in C?

Compile and link the file with your reimplementation ( override. c ). This allows you to override a single function from any source file, without having to modify the code. The downside is that you must use a separate header file for each file you want to override.

What is function overriding explain with example?

Example 1: C++ Function Overriding So, when we call print() from the Derived object derived1 , the print() from Derived is executed by overriding the function in Base . Working of function overriding in C++ As we can see, the function was overridden because we called the function from an object of the Derived class.

When function overriding is implemented in OOP?

Function overriding is a concept in object-oriented programming which allows a function within a derived class to override a function in its base class, but with a different signature (and usually with a different implementation).


1 Answers

You can't override the function, but you can make an extension method for this:

public static class StringExtensions {
     public static bool ContainsAny(this string theString, IEnumerable<string> items)
     {
         // Add your logic
     }
}

You'd then call this just like a normal method on a string, provided you reference the assembly and include the namespace:

String helloworld = "Hello World";
String[] items = new string[] { "He", "el", "lo" };

if (helloworld.ContainsAny(items)) { 
   // Do something
}

(Granted, you could call this "Contains", like the standard string method, but I would prefer to give it a more explicit name so it's obvious what you're checking...)

like image 86
Reed Copsey Avatar answered Oct 21 '22 11:10

Reed Copsey