extension-methods

C# Extension Method to return random alphabet

How to develop an extension method that may return random character(single character) from alphabet (a,b,....z). public static char RandomLetter(this char randomchar) { } ...

C# - Running Total using Aggregate()

This question was asked at interview.I need to have running total (only using Aggregate() ) from array (i.e) int[] array={10,20,30}; Expected output 10 30 60 when i use Aggregate (I applied some worst logic) array.Aggregate((a, b) => { Console.WriteLine(a + b); return (a + b); }); 1) It prints 30,60 ,for me there is no use of ...

C# -Termination in Aggregate( )

From the following simulation int[] amountWithdrawal = { 10, 20, 30, 140, 50, 70 }; amountWithdrawal.Aggregate(100, (balance, withdrawal) => { Console.WriteLine("balance :{0},Withdrawal:{1}", balance, withdrawal); if (balance >= withdrawal) { return balance - withdrawal; } else return balance; } ); I want to terminate the...

Method signature for IList<T>.Split() extension method

I'd like to be able to write the following code: // contains 500 entries IList<string> longListOfStrings = ... // shorterListsOfStrings is now an array of 5 IList<string>, // with each element containing 100 strings IList<string>[] shorterListsOfStrings = longListOfStrings.Split(5); To do this I have to create a generic extension met...

How to create extension methods for Types

I am writing an extension method for parsing JSON string for any given type. I wanted to use the method on types instead of instances like many examples we already know, but I somewhat feel it is not supported by Visual Studio. Can someone enlighten me here? The following is the method: public static T ParseJson<T>(this T t, string str)...

How to create a flexible extension method for generic lists?

Hello, I have 2 objects Project and License. They both inherit from the object Entity (abstract class). Now I have an extension method "GetNewId" that contains logic to get the next id in a list of entities. I've defined this method as an extension method, but the problem is that List (which is also a list of entities) and List don't ...

Is it possible to refactor this extension method?

I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument) where T : class { if (value == null) { throw new ArgumentNullException(argument); } } and this is an example of its usage.... // Note: I've poorly named the argument, on purpose, for this question...

Collection Randomization using Extension Method

Possible Duplicate: C#: Is using Random and OrderBy a good shuffle algorithm? I want to create an extension method which should shuffle the items in the collection. Can i improve the following? public static IList<T> RandomList<T>(this IList<T> source) { if (source.Count <= 0) throw new ArgumentException("No Item to Random...

Object To DataView or DataSet or DataTable and back to object

We have a mish-mash app with a legacy module that still uses DataSets, DataViews and DataTables however we have most of the the databases ORMed except the DB for this Module. I was wondering if someone could give me pointers as to how to go about building extensions like /* generates a dataset called CustomerDS with DataTable called Cu...

Extension Method ConvertAll

What is the proper use of ConverAll ? Will it convert one type to another type? like List<int> intList = new List<int>(); intList.Add(10); intList.Add(20); intList.Add(30); intList.Add(33); var query= intList.ConvertAll(x=>(double)x); for this i can use cast or OfType<>. ...

How to call extension method "ElementAt"of List<T> with reflection ?

Hi all, I have problem that after creating object "oListType01" of type List < MyClass01 > and after assigning it to the another objet "oObjectType " of type "object" I can not access any more function "ElementAt(1)". I tried by using reflection but I am always getting exception(parameter conflict) in "Invoke" method. Does anyone knows...

How to write an extension method that returns a dynamic object?

I was thinking about how Regex.Match.Group wants to be dynamic: Regex.Match (...).Groups["Foo"] would like to be: Regex.Match (...).Groups.Foo I thought about writing an extension method that would allow: Regex.Match (...).Groups().Foo And tried writing it this way, but this isn't allowed (';' required by 'static dynamic') pu...

Extension methods on interfaces

I have two interfaces IDto1 and IDto2. IDto2 inherits IDto1. Both interfaces are for DTOs and so I wish to keep "complex" code out of their implementations - I do this by putting a single extension method Initialize in a static class for each interface. So I end up with the following types IDto1, IDto2, IDto1Extensions, IDto2Extensions....

Experience with fluent interfaces? I need your opinion!

Sorry for this long question, it is flagged wiki since I'm asking for something that might not have a very concrete answer. If it is closed, so be it. My main question is this: How would you write a fluent interface that isn't fully defined in the base classes, so that programs that uses the fluent interfaces can tack on new words i...

Use Contains() extension method on arrays in C#

There is a Contains() extension method on IEnumerable; In VB I am able to do this: If New String() {"A", "B"}.Contains("B") Then ' ... End If What would be the equivalent of this in C#? ...

custom extension methods collide with framework extension methods. Why?

Hi, I have a strange behavior in our application. I want to iterate over the rows of a table (DataTable). I wanted to use the AsEnumerable() extension method from DataTableExtensions class. Sonmething like: foreach(var thing in table.AsEnumerable()) { ... } When it compiles, it complains that I need to reference some ESRI DLLs (GIS a...

Is there a way to write an Extension Method that applies to multiple types?

I'm trying to write an extension method that will add the function HasFactor to the class int in C#. This works wonderfully, like so: static class ExtendInt { public static bool HasFactor(this int source, int factor) { return (source % factor == 0); } } class Program { static void Main() { int i = 5...

How to write correct static methods - multithread safe

As I assume static methods shouldn't be writen like the first snippet , or am I wrong ? public static class ExtensionClass { private static SomeClass object1; private static StringBuilder sb; private static string DoSomething() { sb.AppendLine(object1.SomeValue); } public static string ExtensionMethod(this HtmlHelper helper,...

Extension Methods on both IList and IEnumerable with the same name?

I have written some extension Methods to convert an IEnumerable and an IList into a string. Now, as IList inherits from IEnumerable, I have to name them differently. I just wonder if there is a way to avoid that? Can I have an extension Method on an IEnumerable and then a different one on an IList with the same name and the same signatu...

Extension methods on a 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) ...