extension-methods

How can I write extensions for Session without having to write separate methods for HttpSessionState and HttpSessionStateBase?

I wrote the following extension methods for Session so that I can persist and retrieve objects by their type. This works well for my solution, but I ended up having to duplicate my extension methods to cover the old HttpSessionState and the new HttpSessionStateBase. I'd like to find a way to get these back down to one set that covers bot...

Extension methods on an interface; delegates

I'm trying to get around dual inheritance in C# by re-implementing one of the parent classes as an interface with extension methods. The problem I'm encountering is that event EventHandler<EventArgs> Disconnecting; public static void OnDisconnected(this AutoConnectClientBase target) { target.ClientConnectionStat...

How to adapt a simple String Extension to support SQL

I've got a C# string extension that really makes life easy, but I am getting the exception: Method 'System.String ToUrlString(System.String)' has no supported translation to SQL. I've seen this as a common problem and some people have found ways around fixing this for their method, but I'm not sure it's possible for mine. It's just a ...

Extension methods and a GetBytes implementation

I'd like to use a GetBytes extension method on the List class... public static class Extensions { public static byte[] GetBytes<T>(this ICollection<T> col) { List<byte> bytes = new List<byte>(); foreach (T t in col) bytes.AddRange(BitConverter.GetBytes(t)); return bytes.ToArray(); } } W...

Avoid double control search in LINQ query

I have a Dictionary<string, bool> where key - control's ID and value - it's visible status to set: var dic = new Dictionary<string, bool> { { "rowFoo", true}, { "rowBar", false }, ... }; Some of controls can be null, i.e. dic.ToDictionary(k => this.FindControl(k), v => v) will not work because key can't be null. I can do ...

Extension methods overridden by class gives no warning

I had a discussion in another thread, and found out that class methods takes precedence over extension methods with the same name and parameters. This is good as extension methods won't hijack methods, but assume you have added some extension methods to a third party library: public class ThirdParty { } public static class ThirdPartyEx...

Disposing an IDisposable in Linq

(This is a follow on from a comment on an answer to this question) 18 months after I posted it, someone spotted a bug in one of my Linq examples, where I use an IDisposable half way through a method chain, which never gets disposed. I attempted to write an extension method to handle this: public static IEnumerable<R> Using<T, R>( ...

How to use indexers with Extension Methods having out parameter and function calls

Is it possible to use indexers with extension methods. eg. Consider it as an example only. public static object SelectedValue(this DataGridView dgv, string ColumnName) { return dgv.SelectedRows[0].Cells[ColumnName].Value; } EDIT usage mygrid.SelectedValue("mycol") How to use it as an indexer mygrid....

C# Extension Method on Type With Generic Type Argument

I’m looking at ways to improve the consistency, brevity, and readability of some code in the application I’m working on. The starting code looked something like this: context.GetGraphType<Bar>().Subscribe<Fizz>( (instance, evt) => e.Execute((Bar)instance.Instance) ); There are a number of nearly identical lines of code like the ...

Extension method for Int32 in C#

I envisage the ability to write fluent code that adds meaning to numbers within codebases. Say you wanted a number to represent a distance in miles. You'd have something like: Usage: var result = myMethod(100.Miles()); I think this would be much more readable than simply passing in the int, plus you could presumably apply bounds chec...

Scala equivalent of CSharp’s Extension Methods ?

In C# you can write: using System.Numerics; namespace ExtensionTest { public static class MyExtensions { public static BigInteger Square(this BigInteger n) { return n * n; } static void Main(string[] args) { BigInteger two = new BigInteger(2); System.Console.WriteLine("The square of 2 is " + two.Squar...

Odd way of implementing the MD5 function in C#

I was looking for extension methods and stumbled in to a function that claims to mimic how PHP does the MD5 functions. Here's an exact copy(I don't get why the declaration and initialization of the Crypto were done separately...) public static string MD5(this string s) { MD5CryptoServiceProvider provider; provider = new MD5Crypt...

How to override functions from String class in C#.

For example, I need to see if a string contains a substring, so I just do: String helloworld = "Hello World"; if(helloworld.Contains("ello"){ //do something } but if I have an array of items String helloworld = "Hello World"; String items = { "He", "el", "lo" }; I needed to create a function inside the String class that would r...

Extension Methods & Interfaces

All of my classes implement an interface IPrettyPrint. With extension methods I can add a PrettyPrint method to a double e.g. (List<T>, ...) but is there any possibilty that double supports IPrettyPrint aswell? Anyone that likes to use the PrettyPrint method can rely only on IPrettyPrint. ...

Extending Date class in ruby

Hi All, I want to extend Date class in ruby.Actually what I want to do is that add a method to Date class. Following is an example I want to add 'my_simple_method' method to Date class def my_simple_method puts 'this is from my_simple_method' end after adding this users should be able to call this method as date_obj = Date.parse...

Writing an extension method for type T; how can I add a type constraint for a field of T?

Initial situation: I am working with a proprietary framework (ESRI's ArcGIS Engine) which I want to extend with some new functionality. I've chosen to use extension methods in C# for this. Shown below are the parts of the framework API that are relevant to this question: +------------------------+ IGeometry |...

Enumerable LINQ extensions are hidden on strings... why and how ?

Possible Duplicate: Why doesnt VS 2008 display extension methods in Intellisense for String class Hi all. Yesterday I noticed that Enumerable LINQ exstensions are hidden on strings (I mean hidden from the intellisense). We all know string is an IEnumerable<char>, so automatically it should get Enumerable extensions, and actu...

ASP.NET MVC: files containing extension methods inside App_Code must have Build Action "None"?

This seems to be the only thing that works: If a .cs file is inside App_Code... And does not contain extension methods: set the build action to "Compile"; otherwise no other source code in the project knows of its existence. And contains extension methods: set the build action to "None"; otherwise you get an error that the reference t...

ef and linq extension method

Hi I have this sql that i want to have written in linq extension method returning an entity from my edm: SELECT p.[Id],p.[Firstname],p.[Lastname],prt.[AddressId],prt.[Street],prt.[City] FROM [Person] p CROSS APPLY ( SELECT TOP(1) pa.[AddressId],a.[ValidFrom],a.[Street],a.[City] FROM [Person_Addresses] pa LEFT OUTER JOIN...

Understanding the extention ElementAt(index)

Hi Consider this code: int size = 100 * 1000 * 1000; var emu = Enumerable.Range(0, size); var arr = Enumerable.Range(0, size).ToArray(); when I call emu.ElementAt(size-10) and arr.ElementAt(size-10) and measure the time the arr is much faster (the array is 0.0002s compared to IEnumerable 0.59s). As I understand it, the extention me...