extension-methods

An analog of String.Join(string, string[]) for IEnumerable<T>

class String contains very useful method - String.Join(string, string[]). It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with "<br />" or Environment.NewLine. So I want to add an empty r...

How do I declare a method in C++/CLI that will be seen as an extension method in C#?

I'm writing a .NET assembly in C++/CLI to be used in our C#-based application. I'd like the application to see some of the C++ methods as extension methods. Is there some attribute I can apply to the declaration to specify that a method should be seen as an extension method from C#? ...

.NET MVC - How to create HtmlHelperExtension in VB instead of C#?

Hi, i need to create a HtmlHelperExtension in VB instead of C#. I cannot find any example showing how it's done. Since static classes don't exist in VB (modules are used instead), I'm not really sure on how to create a HtmlHelperExtension... This is what I figured out, but it doesn't seem to work... Public Module HtmlHelperExtensions ...

Take a sequence of elements from an array from i to j using C# and extension method

I have an Array<string>. I have to take all elements from i to j. How can I make this using an extension method? ...

Extension methods defined on value types cannot be used to create delegates - Why not?

Extension methods can be assigned to delegates that match their usage on an object, like this: static class FunnyExtension { public static string Double(this string str) { return str + str; } public static int Double(this int num) { return num + num; } } Func<string> aaMaker = "a".Double; Func<string, string> doubler = FunnyExtensio...

Creating a generic method for converting string to nullable numbers

I have 3 similar functions, that only change based on numeric type: <Extension()> _ Public Function ToNullableShort(ByVal str As String) As Short? Dim intReturn As Short If Short.TryParse(str, intReturn) Then Return intReturn Else Return Nothing End If End Function <Extension()> _ Public Function ToNulla...

What is better to use: Dictionary<>.ForEach(pair =>) or Dictionary<>.Keys.ForEach(key =>) ?

If I have access only keys from a Dictionary<TKey, TValue> what is better to use: Dictionary<TKey, TValue>.ForEach(pair => action(pair.Key)) or Dictionary<TKey, TValue>.Keys.ForEach(key => action(key)) Which method is more 'best-practice' ? Speed in both cases I think seems to be very similar. ...

Invoke Generic Extension method on an Object ?

Hi, I have created a Generic Extension method for DataRow object. The method takes no argument. I want to Invoke the Generic method through Reflection using MethodInfo. I can do this for Normarl public methods, but somehow I cannot get the reference of the Generic Extension method. I've read this question on SO which somehwat relates t...

Naming conventions for extension method namespaces and sponsor classes

What naming conventions are you using for namespaces and sponsor classes? (i.e. the classes that hold extension method definitions) Is there a standard/recommended .NET Framework naming convention? (the "Framework Design Guidelines, 2nd Edition" book only gives guidance on what namespaces not to use). ...

PHP Object Extension Question

So I have an item class as follows: class Item { private $db; private $data = array( 'AltItem1' => null, 'AltItem2' => null, 'BaseUOM' => null, 'Category1' => null, 'Category2' => null, 'Category3' => null, 'Category4' => null, 'Iden' => null, 'IsHCS' => null, 'ItemDesc' => nul...

How does NerdDinner's AddModelErrors work?

I'm going through the NerDinner free tutorial http://nerddinnerbook.s3.amazonaws.com/Intro.htm I got to somewhere in Step 5 where it says to make the code cleaner we can create an extension method. I look at the completed code and it has this to use the extension method: catch { ModelState.AddModelErrors(dinner.Get...

Threadsafe Generic Extension method usage syntax problem

Here's my extension method for invoke on a control: public static void Invoke<T>(this T c, Action<System.Windows.Forms.Control> DoWhat) where T:System.Windows.Forms.Control { if (c.InvokeRequired) c.Invoke(o=> DoWhat(c) ); else DoWhat(c); } ds is a strongly typed dataset. This works: Action<DataGridView> a = row => row...

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

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,...

Use of extension methods to enhance readability

What is the general thinking on the use of extension methods that serve no purpose other than enhancing readability? Without using extension methods we might have the method IEnumerable<DependencyObject> GetDescendents(DependencyObject root) {} that could be called with var descendents = GetDescendents(someControl); or foreach (v...

Custom HtmlHelper extension method not available in View?

I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but when I try to use the method in my view I get the error 'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'. Can anyone tell me where I have gone wrong? Helper module: Imports System.Runtime...

FormatProvider vs. extension method vs. new class

I was wanting to output an integer to roman numerals and ran across this answer by Jesse Slicer. It is an extension method, but I was wondering about taking advantage of ToString(string, IFormatProvider) to do something like int a = 10; string b = a.ToString("RN", provider); // OR string c = string.Format(provider, "{0:RN} blah foo", a...

How do I use an extension method in an ASP.NET MVC View?

How do I access an extension method in an ASP.Net MVC View? In C# I do using MyProject.Extensions; and I remember seeing an XML equivalent to put in a view, but I can't find it anymore. ...

How do I convert IEnumerable to a custom type in C#?

I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my...

Fastest way to convert a list of strings into a single concatenated string?

I have some LINQ code that generates a list of strings, like this: var data = from a in someOtherList orderby a select FunctionThatReturnsString(a); How do I convert that list of strings into one big concatenated string? Let's say that data has these entries: "Some " "resulting " "data here." I should end up w...

Extension methods versus inheritance

Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times? Thanks! ...