extension-methods

Delegate as first param to an Extension Method

纵饮孤独 提交于 2019-12-04 02:35:09
Ladies and Gents, I recently tried this experiment: static class TryParseExtensions { public delegate bool TryParseMethod<T>(string s, out T maybeValue); public static T? OrNull<T>(this TryParseMethod<T> tryParser, string s) where T:struct { T result; return tryParser(s, out result) ? (T?)result : null; } } // compiler error "'int.TryParse(string, out int)' is a 'method', which is not valid in the given context" var result = int.TryParse.OrNull("1"); // int.TryParse.OrNull<int>("1"); doesnt work either // compiler error: type cannot be infered....why? var result2 = TryParseExtensions.OrNull

Extension methods on a static class? [duplicate]

妖精的绣舞 提交于 2019-12-04 02:28:20
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) You can't have extension methods on static classes because extension methods are only applicable to instantiable types and static classes cannot be instantiated. Check this code.. public static bool IsEmail(this string email) {

c# Extension methods - design patterns

*爱你&永不变心* 提交于 2019-12-04 02:06:10
I would like to know if C# extension method is based on any existing design pattern. A design pattern is simply a well known paradigm, i.e. "when you want to achieve X, do Y". A well known paradigm in object-oriented languages such as C# is "when you want to act on the state of an object, call a method on an instance of it". However, before extension methods were created, you could not call your own method on an instance of an object that you could not add an implementation to (e.g. interfaces because they cannot have implementations, or library classes because they are already compiled).

Extending a type in C++

a 夏天 提交于 2019-12-04 01:39:01
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 myoption { // Some functionality private: std::optional<T> impl; }; My problem is that every time I

Split C# collection into equal parts, maintaining sort

非 Y 不嫁゛ 提交于 2019-12-04 01:30:16
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 = from name in list group name by i++ % parts into part select part.AsEnumerable(); return splits; } int

Is it possible to create an extension method to format a string?

牧云@^-^@ 提交于 2019-12-04 00:46:11
问题 the question is really simple. How do we format strings in C#? this way: string.Format("string goes here with placeholders like {0} {1}", firstName, lastName); Now, is it possible to create an extension method to do it this way? "string goes here {0} {1}".Format(firstName, lastName); That's all. 回答1: Well, it's more complicated than it looks. Others say this is possible, and I don't doubt them, but it doesn't seem to be the case in Mono. There, the standard overloads of the Format() method

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

邮差的信 提交于 2019-12-04 00:38:54
Closed. This question is off-topic. It is not currently accepting answers. Learn more . 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 more acceptable than class based multiple inheritance that so many people seem to find so

Can I use Extension Methods inline in an ASPX page?

╄→гoц情女王★ 提交于 2019-12-04 00:36:35
Is it possible to do something like this inline in an ASPX page? <%= Me.SomeExtensionMethod() %> I can't seem to figure out how to get this to work properly. I'm receiving an error saying that "SomeExtensionMethod" is not a member of the current Page object. I've added the necessary <%@ Import Namespace="..." %> directive at the top of my page. This Does work in code-behind. This isn't vitally important, but it would be good to know how to do in the future. Thanks! Michael Haren Try closing the .aspx page and opening it up again as per this answer . If that improves things at all (e.g. enable

extension method on type and nullable<type>

*爱你&永不变心* 提交于 2019-12-04 00:20:54
问题 For sake of simplicity, let's assume I want to write an extension method for the type int? and int: public static class IntExtentions { public static int AddOne(this int? number) { var dummy = 0; if (number != null) dummy = (int)number; return dummy.AddOne(); } public static int AddOne(this int number) { return number + 1; } } Can this be done using only 1 method? 回答1: Unfortunately not. You can make the int? (or whichever nullable type you are using) method call the non nullable method very

Proper way of testing ASP.NET Core IMemoryCache

混江龙づ霸主 提交于 2019-12-04 00:17:28
I'm writing a simple test case that tests that my controller calls the cache before calling my service. I'm using xUnit and Moq for the task. I'm facing an issue because GetOrCreateAsync<T> is an extension method, and those can't be mocked by the framework. I relied on internal details to figure out I can mock TryGetValue instead and get away with my test (see https://github.com/aspnet/Caching/blob/c432e5827e4505c05ac7ad8ef1e3bc6bf784520b/src/Microsoft.Extensions.Caching.Abstractions/MemoryCacheExtensions.cs#L116 ) [Theory, AutoDataMoq] public async Task GivenPopulatedCacheDoesntCallService(