How to override or extend .net main classes. for example
public class String
{
public boolean contains(string str,boolean IgnoreCase){...}
public string replace(string str,string str2,boolean IgnoreCase){...}
}
after
string aa="this is a Sample";
if(aa.contains("sample",false))
{...}
is it possible?
Since String is final there is no way anyone can extend String or override any of String functionality. Now if you are puzzled why String is immutable or final in Java.
No. Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.
The String class is sealed so you can't inherit from it. Extension methods are your best bet. They have the same feel as instance methods without the cost of inheritance.
public static class Extensions {
public static bool contains(this string source, bool ignoreCase) {... }
}
void Example {
string str = "aoeeuAOEU";
if ( str.contains("a", true) ) { ... }
}
You will need to be using VS 2008 in order to use extension methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With