Methods that extend a given type, adding functionality to any instance of that type, even if the type in question is defined in a separate assembly. For example, a "Disemvowel" extension method could be written to add disemvoweling functionality to any "String" object.

learn more… | top users | synonyms

6
votes
3answers
246 views

Checking whether relations are equal to foreign fields using LINQ Any()

Can you find a more understandable way to write the following one-liner? ...
3
votes
3answers
96 views

Custom struct design: Range

I had a need for some inventory management in a recent project. I decided to create a custom struct for managing the concept of a number range. It allows for easy navigation of a collection. It ...
2
votes
1answer
29 views

Extension methods to get direct reports from user

I have created a few extension methods to get the direct reports of a UserPrincipal as UserPrincipals themselves. This saves the ...
8
votes
5answers
123 views

Removing exact instances of elements in one list from another

Basically, here's the problem statement: Given an IEnumerable<T> source and an IEnumerable<T> exceptions, return ...
7
votes
1answer
138 views

Extension method splitting string on each capital letter

I'm creating my own game and for some statistics I need to get the names of specific objects. The problem was that I had them declared like "SmallFireBall" but I wanted them to appear like"Small Fire ...
2
votes
1answer
45 views

ExtensionMethods for iterating hierarchically data structures depth/breath-first

I wrote a few simple extension methods that allow to traverse a hierachically data structure depth-first or breath-first using LINQ: Usage: ...
5
votes
1answer
86 views

Validation extensions v2

I have still another version of my validation extensions. I've reworked it and added some new features. It doesn't relay on expression trees any more but as a compensation the same extensions can be ...
0
votes
1answer
24 views

Iterate through a 2D array. Kotlin extension method

Simple extension method for Kotlin. Should perform an action on every element in a 2d array. i.e. An array of arrays. I have made the assumption that because the receiver type is defined as an array ...
3
votes
1answer
68 views

Validation extensions

I've been working on another timesaver because I really really don't like to type all those validation ifs and exceptions over an over again. What I came up with ...
3
votes
1answer
62 views

Primitive Extensions - Replaces static primitive methods

I got annoyed having to do things like string.IsNullOrEmpty(myString) when it seemed as if myString.IsNullOrEmpty() would ...
2
votes
2answers
61 views

Custom map on `Array`

I have the following problem: Given an Array, apply a certain method x to each Array ...
0
votes
1answer
86 views

Data layer using extension methods

I've started to use extension methods to provide data layer functionality in our project. Our project is an MVC website using Code First Entity Framework. So, I've done things like this: ...
2
votes
1answer
55 views

XCTestCase#waitFalseExpectationUntilTimeout implementation

I am working on an iOS project and I'm in charge of testing most parts of it. As I write some tests, I often have the need to wait for an asynchronous method to finish, and then test that something ...
12
votes
2answers
150 views

Something like a LINQ provider

Ok, before you ask: yes, I need to do this. Sort of. I'm wrapping a 3rd-party API for data access, and I can't use an ORM, so I'm implementing this kind of thing: ...
4
votes
1answer
268 views

C# Extension Properties

I am doing some math - combining, reusing, extending different analyses methods over the same original data set. It would be reasonable to use C# extension methods in my case instead of defining ...
12
votes
2answers
717 views

Extension method to list enum values

I have the following enum extension method: ...
11
votes
5answers
596 views

Solving Project Euler Problem 1 using extension methods

As a self taught developer I never did any of the Project Euler problems, so I decided to just start with Problem 1 which states If we list all the natural numbers below 10 that are multiples of ...
1
vote
1answer
83 views

Variable length string validation against whitelisted characters

I need to validate a variable length string against some white-listed characters. The algorithm that I have works but I have a sneaking suspicion that there is a more elegant solution. The code below ...
3
votes
4answers
217 views

IEnumerable Extensions Linq AllOrDefault() Each()

I recently decided to try and write my own implementation of Linq, which then lead me on to trying to solve some of the problems we have in our code base at work. Our code is littered with the ...
3
votes
2answers
95 views

IEnumerable extension method that ingests SqlCommand and returns query results

The following ingests a preexisting SqlCommand and returns the values. A couple caveats right now. 1 ) Type properties must be named identical to the SQL column ...
6
votes
1answer
386 views

Helper for DropDownLists with extension method

Is the following helper idiomatic ASP.NET MVC? All the built-in DropDownListFor helpers only accept ...
4
votes
4answers
2k views

Get string truncated to max length

This is an extension method to get a string that is truncated to a maximum length. Any comments? ...
3
votes
2answers
276 views

Checking if an array is initialized and allocated in VB.NET

I am creating an add-in for a piece of software our company uses, so I'm working pretty extensively with that program's API. There are many COM object members which return or contain an array that I ...
2
votes
1answer
86 views

An extension method to get the current user [closed]

I think that everyone is familiar with the concept of a context (such as HttpContext in ASP.NET) where you can put some shared ...
3
votes
1answer
181 views

Extension method to do Linq Lookups

Does this extension method contain unnecessary work? - Thanks for reviewing Scenario - A series of tables are Key-Value pairs (Int "Id" / nvarChar "Title"). This extension method "dots-off" any ...
2
votes
1answer
307 views

String extension method that truncates at sentence punctuation

I've received a request at work and the senior developer is away on vacation so I can't ask him. The request was to truncate lengthy article titles while maintaining readability. In other words, don't ...
10
votes
2answers
336 views

Lazy String.Split

C#'s String.Split method comes from C# 2.0, and lazy operations weren't a feature back then. The task is to split a string according to a (single) separator. Doing ...
6
votes
3answers
264 views

Extension method to enumerate a hierarchical object

I have a domain object that I have no control over which itself contains a collection of this same type. This is a classical hierarichal representation like in a tree node hierarchy. I would like to ...
14
votes
1answer
2k views

Let's play some Swift Poker

Before you can do any sort of card game, you must first write some code to define your deck of cards. One thing I've noticed in looking at some of Apple's Swift interfaces is that they very much so ...
10
votes
1answer
376 views

Object to object mapping verification - is this extension method useful?

In our .NET tests we use NSubstitute & ExpectedObjects. Testing object expectations involves hand crafting large anonymous ...
5
votes
2answers
1k views

Extension methods for safely firing events

I wrote a set of extension methods for the EventHandler class that add the method Fire, which raises the event after creating a ...
7
votes
1answer
2k views

Extension methods for Daylight Savings Time dates

Just wondering if anyone had any suggestions for improvements. I created these extension methods so it's easier to get the start and end dates of Daylight Savings Time from the ...
2
votes
1answer
538 views

AsyncLazy disposal

I've made this extension method. Its purpose is to trigger the disposal of a value, stored in a Nito.AsyncEx AsyncLazy, as authored by @StephenCleary. Is this an ...
5
votes
2answers
474 views

Helper extension to release Windsor component; not sure if it's over-kill

One of the tenets of Windsor IoC (probably applies to all IoC containers too) is to "release what you explicitly resolve", which admittedly should occur rarely. But we have a fair few ...
4
votes
1answer
513 views

IndexOrDefault that functions like FirstOrDefault

I don't know why this is not in the .NET Library, but I need to be able to use an index against a list and not have it throw an exception if it is outside the bounds of the list. This is what I came ...
7
votes
2answers
107 views

Output in one window

Can anyone give me feedback please? I used what I have learnt so far, mainly methods, loops and arrays. I would like you view based on these topics, however, comments on how to improve are welcome. <...
2
votes
1answer
74 views

JavaScript mixins, extending and super methods

I've read a lot of stuff about mixins, inheritance and such, and in the end I came up with this solution for extending a class with multiple mixins. I haven't seen this anywhere else... Is this a ...
0
votes
1answer
212 views

JavaScript mixins - extending and super methods [closed]

I've read a lot of stuff about mixins, inheritance and such, and in the end I came up with this solution for extending a class with multiple mixins. I haven't seen this anywhere else... ...
3
votes
1answer
4k views

Folder browser dialog to remember recent folder. Which method is better?

which of the following code you prefer and why? The scenario is, I need to pass the recent folder path to folder browser dialog and if user selects any folder, need to save it back. The value is ...
5
votes
2answers
4k views

Extension method to return a default value for any null value

I'm creating an extension method that can be used on any type. The idea is that if this method is called, it checks if value is null. If null, it needs to return a default instance of the specified ...
4
votes
3answers
915 views

Lazy man's IEnumerable extension verification method

I often find myself doing this (or similar) with Lists and other collection types: ...
3
votes
1answer
1k views

Validating an entity using a dynamic list of predicates

I have a generic extension method for validating my entities. The main idea is to be able to specify at runtime (context related) the criteria for validating a specific entity (with the end goal of ...
4
votes
2answers
430 views

Get array of pressed buttons using extension method

In my game, I need to detect whether any buttons/keys are pressed at all before I try to process any input. This is easy enough for keys since the XNA library provides a ...
7
votes
3answers
590 views

Ternary extension method

I created the following HtmlHelper extension method for my Asp.Net MVC Razor views because the ternary syntax sucks when you need to use it intermixed with markup. Is there a better way to write this ...
8
votes
1answer
37k views

Converting List to a DataTable and/or DataSet Extension Methods

Can someone help me improve this code? I and trying to have a couple extension methods to convert strongly-typed lists to a DataSet and ...
3
votes
1answer
2k views

A genric extension method to filter Linq-EF queries

I have various types of EF entities, all of them have a navigation property called "Employee". When generating reports the user will have the option to filter the report according to the different ...
8
votes
2answers
10k views

Digit to words converter

This is one of the longest programs I have made with methods and I think I must be doing this rather inefficiently. Any comments to improve would be appreciated. ...
6
votes
3answers
168 views

Object inheritance

I have written some example code to test object inheritance, but I'm not sure if it's really the best way for an object to inherit another's functions (like Java's ...
5
votes
3answers
661 views

Are private extension methods a bad thing to use?

In C# I have the following two extension methods. ...
1
vote
4answers
2k views

Extension Methods for Trimming List

I'm currently working on a project that I had to create basically a running queue of items. My thought was to do this by a list, but surprisingly there weren't any methods to remove the first sequence ...