extension-methods

Extension method resolution

I wrote an extension method for String to get a char argument, string.Remove(char). But when I used this, it instead called the default string.Remove(int) method. Shouldn't the presence of an actual method have higher priority than an implicit conversion? ...

.NET List.Distinct

I'm using .NET 3.5. Why am I still be getting: does not contain a definition for 'Distinct' with this code: using System.Collections.Generic; //.. . . . . code List<string> Words = new List<string>(); // many strings added here . . . Words = Words.Distinct().ToList(); ...

C# cannot implicitly convert type T to type T

my method: public TableFilled<TKey, TRow> getTera() { Func<TablesFilled<TKey,TRow>> _getTera=new Func<TablesFilled<TKey,TRow>>( ()=>{return (TablesFilled<TKey,TRow>) chGetTera();}); //Above does not compile says: Cannot convert type //'AcapsVerify.FunctionalTables.TableFilled<TKey,TRow>' to //'AcapsVerify.F...

AS3: Extending The Dictionary Class - Accessing Stored Data

So I want to extend the dictionary class. Everything works so far except that in some of my methods that need to reference the dictionary's content I make a call like: this[ key ] It doesn't like that. It just tells me that there's no property 'key'. Is there a way to way to access the data within this class? Also, I'm using an inte...

.Net class helper method

Is there a way to add a method to a class, but allow it still be inherited by the base class? I have the following public class ListWithRandomize<T> : List<T> { public void Randomize() { // Randomize function} } I'm going to have a bunch of List objects that will need to be randomized. Is it possible to have a List object that I...

Conversion Class Extensiblity (Extension Methods vs Parital Class)

I am creating a conversion class for frequency, and I wanted to allow my team to be able to add additional conversions when they are needed. Frequency myFrequency = new Frequency(100, MHz); double value = myFrequency.InKhz(); The source code for the class will not be include in future projects, so I will either have to have the clas...

Possible overuses of Extension Methods

I'm just coming over to .NET 3.5, and I've started using Extension Methods. My first thought is that these are really cool. I want to use them everywhere. I feel like I'm wielding an "Extension Method" hammer, and every piece of code looks like a nail. I should hold myself back and ask - can anyone think of situations where you could...

How do you create an extension in C#?

Hey SO, I wrote this extension for stringbuilder in VB.NET: Imports System.Runtime.CompilerServices Public Module sbExtension <Extension()> _ Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _ ByVal format As String, _ ByVal arg0 As Obj...

Is it possible to extend arrays in C#?

I'm used to add methods to external classes like IEnumerable. But can we extend Arrays in C#? I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional. Not related to http://stackoverflow.com/questions/628427/how-to-extend-arrays-in-c ...

[C# 3.0] Extension Methods and its implications in Software Engineering (or is it really a good idea?)

Right, so MS introduces Extension Methods for C# 3.0. Basically, it allows you to add new methods to an existing classes without having to subclass from it or change its implementation. Immediately alarm bells go off in my mind. 1) You start implementing your own extension methods for a library or standard library classes. Now it is not...

Approach for using 'params' in a generic type definition

This question got me thinking on how one could approach writing a method that would need to contain a variable list of type parameters. One approach would be to accept params Type[] as part of the arguments, such as: public static bool IsOneOf(this object obj, params Type[] types) { return types.Contains(obj.GetType()); } Howeve...

Evil use of Maybe monad and extension methods in C#?

I've been thinking about the null propagation problem in .NET, which often leads to ugly, repeated code like this: Attempt #1 usual code: string activeControlName = null; var activeForm = Form.ActiveForm; if (activeForm != null) { var activeControl = activeForm.ActiveControl; if(activeControl != null) { activeContro...

C#: Adding extension methods to a base class so that they appear in derived classes

I currently have an extension method on System.Windows.Forms.Control like this: public static void ExampleMethod(this Control ctrl){ /* ... */ } However, this method doesn't appear on classes derived from Control, such as PictureBox. Can I make an extension method that appears not only in Control, but for classes derived from Control...

Best practices: C# Extension methods namespace and promoting extension methods

Hi, I know there exists already a post, describing nearly the same, but I think mine is a bit different. What I would like to know is how you organize your extension methods in terms of assigning the namespace. Currently - for the extension methods in our framework - I use the following namespace pattern MyCompany.Web.Utils and ins...

Where is the "Fold" LINQ Extension Method?

I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example: double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor); Unfortunately, I can't get this to compile, either in their example or in my own code, and I c...

Doesn't C# Extension Methods allow passing parameters by reference?

Is it really impossible to create an extension method in C# where the instance is passed as a reference? Here’s a sample VB.NET console app: Imports System.Runtime.CompilerServices Module Module1 Sub Main() Dim workDays As Weekdays workDays.Add(Weekdays.Monday) workDays.Add(Weekdays.Tuesday) Console.WriteLine("Tues...

RadComboBoxItemCollection not wotrking with linq

I'm creating some extension methods and I'm getting some errors with RadComboBoxItemCollection, RadComboBoxItemCollection appears to implement IEnumerable but linq keeps giving me errors saying: "Could not find an implementation of the query pattern for source type 'Telerik.Web.UI.RadComboBoxItemCollection'. 'Where' not found....

Unittesting extension methods

Our team has just started unittesting and mocking, and we have run into some discussion considering extension methods. The question is what is a good approach to testing classes, that make use of extension methods. I.e. we have an Enum like this.. public enum State { [LangID(2817)] Draft = 0, [LangID(2832)] Booked = 1, ...

How do I port an extension in VB.NET to C#?

Hello SO: I wrote an extension in VB.NET for StringBuilder to add a AppendFormattedLine method (so I would not have to use one of the arguments for a new line character): Imports System.Runtime.CompilerServices Public Module sbExtension <Extension()> _ Public Sub AppendFormattedLine(ByVal oStr As System.Text.StringBuilder, _ ...

How to specify parameter for generic list type extension method in c#

Hi, I am trying to make an extension method that will shuffle the contents of a generic list collection regardless of its type however im not sure what to put in between the <..> as the parameter. do i put object? or Type? I would like to be able to use this on any List collection i have. Thanks! public static void Shuffle(this List<??...