extension-methods

Extension methods on a static class? [duplicate]

半城伤御伤魂 提交于 2019-12-05 16:49:32
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Can I add extension methods to an existing static class? I know i can do the below to extend a class. I have a static class i would like to extend. How might i do it? I would like to write ClassName.MyFunc() static public class SomeName { static public int HelperFunction(this SomeClass v) 回答1: You can't have extension methods on static classes because extension methods are only applicable to instantiable types

Split C# collection into equal parts, maintaining sort

守給你的承諾、 提交于 2019-12-05 15:57:37
问题 I am trying to split a collection into multiple collections while maintaining a sort I have on the collection. I have tried using the following extension method, but it breaks them incorrectly. Basically, if I was to look at the items in the collection, the order should be the same when compared to the broken up collections joined. Here is the code I am using that doesn't work: public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts) { int i = 0; var splits =

Extending a type in C++

♀尐吖头ヾ 提交于 2019-12-05 15:43:53
问题 Sadly, UFCS did not make it into C++17 and that left me with a recurring problem: Sometimes I want to give types extra functionality using the method call syntax (without writing global functions). That would especially come handy when dealing with monads. I see two options: One is inheritance, and the other is encapsulation. Because you cannot safely inherit from STL containers, that leaves encapsulation. For example, I want to extend std::optional , so I write: template <typename T> struct

Use system namespaces for class libraries: good or bad

旧时模样 提交于 2019-12-05 15:19:58
Is it a good idea to use "system namespaces" in my class libraries? Sample: namespace System.Web { public static class RequestExtensions { public static bool IsPost(this HttpRequest r) { return string.Compare(r.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) == 0; } } } The advantage: no need to include additional uses-clauses (especially for extension methods), so all becomes available straight after adding reference to the library. The best sample is NUnitEx project (which uses NUnit's namespace). Disadvantages: potential name conflicts. I have to second everyone else who says its a

Extension Method and Razor Page

帅比萌擦擦* 提交于 2019-12-05 14:33:14
I have defined an extension method in app_code like below. public static class Extensions { public static string Hi(this object obj) { return "hi"; } } In the razor page, anything can say Hi :) @Html.Hi(); @Request.Hi(); @this.Hi(); But @Hi() doesn't work. Is there a way to make @Hi() work? SLaks C# only allows you to call extension methods qualified by an object instance. If you have an extension method that extends your type, you can't call it "directly"; you need to write this.ExtensionMethod() . The only way to do what you're asking for is to make a class which inherits WebPage (or

How to hide extension methods from derived classes?

狂风中的少年 提交于 2019-12-05 14:22:26
I have a base abstract class to implement template method pattern. public abstract class OptionalParameter { //Template Method Pattern public string GenerateQueryString() { return this.GenerateQueryStringWithParameters(); } } And I have an extension method for all OptionalParameter type. public static class OptionalParameterExtensions { public static string GenerateQueryStringWithParameters(this OptionalParameter optionalParameters) { } } I inherited a class from OptionalParameter named CalendarEventParameter. public class CalendarEventParameters : OptionalParameter { } When I want to create

Why does C# allow multiple inheritance though interface extension methods but not classes? [closed]

丶灬走出姿态 提交于 2019-12-05 13:58:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 7 years ago . I've checked through other questions and surprisingly this question doesn't seem to have been asked. With Extension methods, interfaces provide limited but true implementation multiple inheritance. This brings with it the Diamond problem, the same as with class based multiple inheritance. Why is this better or

Extension Method for Generic Class [duplicate]

青春壹個敷衍的年華 提交于 2019-12-05 13:32:14
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: C# -Generic Extension Method How do you write a C# Extension Method for a Generically Typed Class Is it possible to declare extension methods for generic classes? public class NeedsExtension<T> { public NeedsExtension<T> DoSomething(T obj) { // .... } } 回答1: To extend any class public static class Extensions { public static T DoSomething<T>(this T obj) { //... } } To extend a specific generic class public

Why does foreach fail to find my GetEnumerator extension method?

守給你的承諾、 提交于 2019-12-05 12:39:24
问题 I'm trying to make some code more readable. For Example foreach(var row in table) {...} rather than foreach(DataRow row in table.Rows) {...} . To do this I created an extension method: namespace System.Data { public static class MyExtensions { public static IEnumerable<DataRow> GetEnumerator( this DataTable tbl ) { foreach ( DataRow r in tbl.Rows ) yield return r; } } } But the compiler still throws foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System

Why can't VB.Net find an extension method on an interface?

只谈情不闲聊 提交于 2019-12-05 12:30:42
I have a C# library that has an extension method, something like: public interface ISomething { ... } public class SomethingA : ISomething { ... } public class SomethingB : ISomething { ... } public static class SomethingExtensions { public static int ExtensionMethod(this ISomething input, string extra) { } } The extension works fine if called from C#, but has an issue if called from an external VB.Net application: Dim something = Me.SomethingManager.GetSomething(key) Dim result = something.ExtensionMethod("extra") This compiles fine but throws an exception at run time: Public member