This question already has answers here:
Can I add extension methods to an existing static class?
(18 answers)
Closed 9 years ago.
How Can I Make Extension Method For System.IO.Path Class what i mean that i need something like below:
Path.GetExtension(sFilePath)
i want to make method:
Path.GetMimeType(sFilePath)
Extension Method:
public static string GetMIMEType(this Path sPath,string sFilePath)
{
string sExtension = Path.GetExtension(sFilePath).ToLowerInvariant();
if (sExtension.Length > 0 && dicMIMETypes.ContainsKey(sExtension.Remove(0, 1)))
{
return dicMIMETypes[sExtension.Remove(0, 1)];
}
return "unknown/unknown";
}
but when compile above code method get error ('System.IO.Path': static types cannot be used as parameters).
Thanks All,
Path is static, and therefore you cannot create an extension method for it. Extension methods require an instance of an object.
This feature is currently not available. you can add extension methods only to an Instance not to Class itself(static).
Feature request already given to Microsoft Static extension methods in C# 4.0
it is not implemented and They may consider it in future or may not.
Related
This question already has answers here:
C# extend indexer?
(6 answers)
Closed 1 year ago.
I am intrested in creating custom indexer for char[,]. It seems to me that it is prohibited, but I am wondering if there is an oportunity. The code that could have solved the problem is:
public static class GeneratorHelpers
{
public static char int[Vector2D position] (this char[,] field)
{
return field[position.X, position.Y];
}
}
The above code does not compile.
Currently, there is no such things as extension properties, operators or indexers in the C# language. The most common pattern for this would be just to take additional parameters to an extension method.
This question already has answers here:
What is difference between extension method and static method?
(2 answers)
Closed 3 years ago.
Given a Class Definition :
Public Static Class Foo
{
public static void Bar(this string A, string B, string C)
{
}
}
What would be the difference between passing the String Explicitly and Implicitly. Viz.
Foo instanceOfFoo="some value";
$(instanceOfFoo).Bar("b","c");
vs
Foo.Bar("a","b","c");
To clarify basis comments, the project I am working on implements a String Extension Logger, which according to my understanding seems a bad choice given that there are a million records of online orders with a lot of columns as varchar(even order_num is a varchar) that translate to strings.
However, since this is implemented by an architect, who seems to be convinced there would be no performance deterioration in using string extension methods, we are forced to use the String Extension method Logger.
I have taken the example as Foo and Bar, but in reality this is
Logger
.Info
.Error etc
There is no difference other than the syntax, in fact the compiler will translate the implicit extension method call to the explicit call to a static method. Extension methods are just very sweet syntactic sugar.
This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static itself
This question already has answers here:
Add new property to string class C# [duplicate]
(3 answers)
Closed 7 years ago.
I don't like the way string behaves and there are a few other things I would like to change.
It appears string cannot be extended because it is a sealed class.
Is there another way? I could copy the source code and make my own class but then it wouldn't be compatible with string, or could I make it compatible?
You could use extension methods to extend String. The link below explains extension methods and has an example of how to add a WordCount() function to String.
https://msdn.microsoft.com/en-us/library/bb383977.aspx
This question already has answers here:
Is there any way in C# to override a class method with an extension method?
(4 answers)
Closed 8 years ago.
Is it possible to overwrite instance methods by an extension-methods? For example, I have an assembly (compiled, without sources) and I want to rewrite some behaviour.
I created some code for test:
public class SomeClass
{
public void MyMethod()
{
Console.WriteLine("Instance method is called");
}
}
public static class ExtMethods
{
public static void MyMethod(this SomeClass c)
{
Console.WriteLine("Extention method is called");
}
}
then I try to use:
SomeClass s = new SomeClass();
s.MyMethod();
It's compiled successfully, IntelliSence marks this method as instance (extension method has another icon)
output says
Instance method is called
none note about extention method exists. So, no way to overwrite instance method, right?
Why the behavior of the compiler and VS is such non-informative? If I develop an extension-method and there exists already the same instance method (and I don't know about it), I can spend hours to understand why behavior is different than expected...
No, you cannot override the instance method with extension methods. Hence, the name 'extension' method, you can only extend the instance methods.
An instance method will always override the extension method.
If you want to force to call the extension method, call it as a regular static method:
ExtMethods.MyMethod(yourClassInstance);
About the why VS doesn't tell you it is a duplicate: actually it can't do that. What if you have an extension method on object. Should VS check all classes and their methods if any method is a duplicate? They simply didn't build the check, it is something you should do yourself.