extension-methods

Calculating Count for IEnumerable (Non Generic)

百般思念 提交于 2019-11-27 19:58:28
Can anyone help me with a Count extension method for IEnumerable (non generic interface). I know it is not supported in LINQ but how to write it manually? The simplest form would be: public static int Count(this IEnumerable source) { int c = 0; using (var e = source.GetEnumerator()) { while (e.MoveNext()) c++; } return c; } You can then improve on this by querying for ICollection : public static int Count(this IEnumerable source) { var col = source as ICollection; if (col != null) return col.Count; int c = 0; using (var e = source.GetEnumerator()) { while (e.MoveNext()) c++; } return c; }

How to add new methods to an existing type in Go?

泪湿孤枕 提交于 2019-11-27 19:13:21
问题 I want to add a convenience util method on to gorilla/mux Route and Router types: package util import( "net/http" "github.com/0xor1/gorillaseed/src/server/lib/mux" ) func (r *mux.Route) Subroute(tpl string, h http.Handler) *mux.Route{ return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h) } func (r *mux.Router) Subroute(tpl string, h http.Handler) *mux.Route{ return r.PathPrefix("/" + tpl).Subrouter().PathPrefix("/").Handler(h) } but the compiler informs me Cannot define new

Extending the C# Coalesce Operator

流过昼夜 提交于 2019-11-27 18:52:21
Before I explain what I want to do, if you look at the following code, would you understand what it's supposed to do? (updated - see below) Console.WriteLine( Coalesce.UntilNull(getSomeFoo(), f => f.Value) ?? "default value"); C# already has a null-coalescing operator that works quite well on simple objects but doesn't help if you need to access a member of that object. E.g. Console.WriteLine(getSomeString()??"default"); works very well but it won't help you here: public class Foo { public Foo(string value) { Value=value; } public string Value { get; private set; } } // this will obviously

C# Extension Method - String Split that also accepts an Escape Character

荒凉一梦 提交于 2019-11-27 18:22:43
问题 I'd like to write an extension method for the .NET String class. I'd like it to be a special varation on the Split method - one that takes an escape character to prevent splitting the string when a escape character is used before the separator. What's the best way to write this? I'm curious about the best non-regex way to approach it. Something with a signature like... public static string[] Split(this string input, string separator, char escapeCharacter) { // ... } UPDATE: Because it came up

Extension Methods vs Static Utility Class [closed]

霸气de小男生 提交于 2019-11-27 18:20:34
I'm looking for some pros and cons for using extension methods over static utility classes in a C# app. For instance, a plus in the extension methods column is the convinience of calling by the class name rather than something like "StringUtils". But a con would be that it can blur the lines between what is in the framework and what isn't. I would say that a pro is that it blurs the lines between what is in the framework and what isn't: you can use your own code as naturally as framework code, operating on framework types. Extension methods shouldn't be used arbitrarily, of course - it's not

How do you write a C# Extension Method for a Generically Typed Class

落花浮王杯 提交于 2019-11-27 18:06:50
问题 This should hopefully be a simple one. I would like to add an extension method to the System.Web.Mvc.ViewPage< T > class. How should this extension method look? My first intuitive thought is something like this: namespace System.Web.Mvc { public static class ViewPageExtensions { public static string GetDefaultPageTitle(this ViewPage<Type> v) { return ""; } } } Solution The general solution is this answer. The specific solution to extending the System.Web.Mvc.ViewPage class is my answer below,

Extension method for List<T> AddToFront(T object) how to?

做~自己de王妃 提交于 2019-11-27 17:12:16
问题 I want to write an extension method for the List class that takes an object and adds it to the front instead of the back. Extension methods really confuse me. Can someone help me out with this? myList.AddToFront(T object); 回答1: List<T> already has an Insert method that accepts the index you wish to insert the object. In this case, it is 0. Do you really intend to reinvent that wheel? If you did, you'd do it like this public static class MyExtensions { public static void AddToFront<T>(this

Does Array.ToArray<>() return the original array if it is the same type?

痴心易碎 提交于 2019-11-27 17:06:07
问题 I deal with a framework on a daily basis where we sometimes provide methods that accept IEnumerable<MyBusinessObject> as a parameter in order to show user interfaces, perform calculations etc. If I pass in an array of MyBusinessObject like so: MyBusinessObject[] myArray = new MyBusinessObject { obj1, obj2, ..., objN }; frameworkClass.MyMethod(myArray); .... public class FrameworkClass { public void MyMethod(IEnumerable<MyBusinessObject> objs) { // Other code that uses the enumerable

Code equivalent to the 'let' keyword in chained LINQ extension method calls

大兔子大兔子 提交于 2019-11-27 16:54:01
Using the C# compilers query comprehension features, you can write code like: var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" }; var result = from animalName in names let nameLength = animalName.Length where nameLength > 3 orderby nameLength select animalName; In the query expression above, the let keyword allows a value to be passed forward to the where and orderby operations without duplicate calls to animalName.Length . What is the equivalent set of LINQ extension method calls that achieves what the "let" keyword does here? Let doesn't have its own operation; it

What's your favorite LINQ to Objects operator which is not built-in?

非 Y 不嫁゛ 提交于 2019-11-27 16:39:02
With extension methods, we can write handy LINQ operators which solve generic problems. I want to hear which methods or overloads you are missing in the System.Linq namespace and how you implemented them. Clean and elegant implementations, maybe using existing methods, are preferred. Timwi Append & Prepend /// <summary>Adds a single element to the end of an IEnumerable.</summary> /// <typeparam name="T">Type of enumerable to return.</typeparam> /// <returns>IEnumerable containing all the input elements, followed by the /// specified additional element.</returns> public static IEnumerable<T>