Questions tagged [extension-methods]

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.

Filter by
Sorted by
Tagged with
6
votes
1answer
73 views

Distinguish between different types of log items in a dictionary by their key

My logging adapter is using a dictionary for storing data that is passed from middleware to middleware. It is a one-liner: ...
1
vote
1answer
47 views

Insert new middleware at the “right” location

I need to have my logger middlewares (nodes of a linked-list) in the right order so that they complement each other and work as desired. Any known middleware can be inserted anytime but the first and ...
4
votes
0answers
87 views

Toggle any application feature ON or OFF - follow-up (v2)

This is the second version of my FeatureToggle service (see previous question). It still has the same purpose which is to help control whether a feature is ...
16
votes
3answers
2k views

ExactlyOne extension method

I often find in codebases something on the order of if (sprockets.Count() > 0) which is easily replaced with LINQ's ...
1
vote
0answers
22 views

Extensible tree header context menu

I have a Qt tree view which has a custom tree header. The tree has a so called mode, by default there are two. During each of the modes a different tree is shown. The mode switcher is presented as a ...
7
votes
5answers
521 views

Grouping into more groups in one iteration

I had a need to group the same dataset in several groups. So instead of repeatedly query the dataset, I made an extension that could do it once. The caveat is, that the result is materialized in ...
5
votes
2answers
481 views

Reducing using/foreach/using nesting with a helper extension

Problem statement There are sometimes foreach scenarios that require deep nesting due to multiple disposable objects involved that look like this: ...
4
votes
3answers
206 views

SkipLast of an IEnumerable<T> - Linq Extension

As my answer to this question, I came up with this solution: ...
6
votes
3answers
268 views

Simple object validator with a new API

Quite some time ago I have created the Simple object validator (see also self-answer). The more I used it the more I thougt its API could be better so I have heavily refactored it and would like to ...
5
votes
2answers
338 views

Fetch and print all properties of an object graph as string

Below I have setup an extension method that takes any object, cycles through its properties, and prints each out to a Console window in the format Name: Value. ...
2
votes
2answers
170 views

Build WHERE clause for search conditions [closed]

Here is a set of classes that are used to build where clause for SQL Server and Oracle for different field types e.g. text, <...
6
votes
1answer
149 views

Enumerate all members and types with specific attributes

I have a use-case where I need to retrieve all members with specific attributes in the class and interface hierarchy - I usually need the first match and apply its rules to child members. The built-in ...
1
vote
0answers
35 views

string.To2DCharArray extension method implementation

I would like to ask a code review for the class below. I am asking this because VS showed a quite low Maintainability Index of 54 and I am wondering if it is good code. I know I could have extracted ...
3
votes
0answers
27 views

Configure multiple application features by common criteria

My FeatureSerivce provides only basic APIs, so it is good in dealing with single features. Like I can configure only one feauture at a time: ...
2
votes
2answers
70 views

Creating keys/names from interfaces to avoid strings

There are many APIs that require some kind of a string key/name. I usually try to avoid using raw strings and prefer to use ...
-1
votes
1answer
63 views

How to divide a large method into 2 smaller methods [closed]

I have created a method that is responsible for the creation and the saving of memes. This is a huge mistake because now I need to use 1 of those functionalities (creating) for a different purpose. I ...
2
votes
2answers
128 views

Formatting a price in Swift using an extension vs. a class

I have a price as a Double, which I should format it as a String. Should I use an extension instead of classical class to ...
1
vote
0answers
68 views

Extension “With” for immutable types

(My code is basically a rewrite of https://github.com/ababik/Remute so much of the credit goes there) The idea is to use lambda expression to provide a general With...
-2
votes
3answers
40 views

Refactorizing overloads with the same scope but different calls [closed]

Well I have 3 methods that are overloads, but in it's scope has the same scope except one method call. ...
5
votes
2answers
336 views

Generic extension to transform a flat nested list to hierarchy list

I have a collection of items in my database with a ParentId property. I'm reading the categories as a flat structure and I would like to convert it to a hierarchy list by populating the ParentCategory ...
3
votes
1answer
59 views

Immutable type updater using a special constructor

I wanted to update some properties of my UriString but since this type is immutable, it wouldn't work. Actually, I just wanted to update the Path property but I don'...
2
votes
1answer
65 views

Squeezing decorators into functional extensions

When you try to use several decorators it can get ugly pretty quickly and you'll end up with: ...
3
votes
1answer
71 views

Determining if there is data left after fetching a page of data

I just faced a problem where I needed to know if extra data was present in a given collection after a Take operation took place. Specifically, this is related to ...
1
vote
2answers
105 views

Converting a String to USD

I'm working on a clientside iOS app for my school that involves students purchasing things, and I wrote a String extension for formatting a ...
3
votes
2answers
540 views

Writing a generic casting extension-method

I'm writing a C# extension-method that converts any object to a type. E.g. the method shall convert a string with value "y" to a ...
1
vote
2answers
473 views

Generic TryParse extension method for the standard value types

I often need to update properties using the out parameter of the standard TryParse methods. However, use of a property as ...
6
votes
2answers
355 views

Parsing small data into tuples

I'd like to make parsing small samples of data more efficient (from the developer point of view). This means, instead of writing the parsing logic each time from scratch when I encounter something as ...
6
votes
2answers
3k views

Split string with adding separator to substring

I wrote my Split() extension, it's main goal is to save delimiters and add them to splitted strings. For example: I have a string ...
5
votes
1answer
58 views

Utilities for Various Builtins #2

This is the final iteration of an iterative review. The previous iteration can be found here: Utilities for various builtins #1 This code solves the following issue: ...
1
vote
1answer
39 views

Utilities for various builtins #1

This is an iterative review. The next iteration is located here: Utilities for Various Builtins #2 This code puts several builtins (And a few non-builtins) into the base classes. ...
3
votes
2answers
303 views

Kotlin extension functions with null checks

Kotlin defines extension functions like also but they are not null checked. That's fine because you might not need the check. In case you do though, do you see any ...
8
votes
4answers
631 views

Selective updates to immutable types

There is a way in Scala to selectively change a field while making a copy of the immutable object: ...
4
votes
2answers
162 views

Initializing an Array of Alternating Values

My goal in this project is to make something that can—as, quickly, effectively, and efficiently as Array(repeating: , count: )—initialize an ...
6
votes
2answers
475 views

Finding MinMax values and returning a tuple as a result

I was coding some tooling stuff when I started to ponder whether the best return type for a Range / MinMax method applied on a IEnumerable method. I am using the value the C# 7 Tuples like below, but ...
4
votes
2answers
525 views

Scheduler built with observables

I sometimes have to schedule some tasks and wanted to have my own reusable scheduler. Since I like the Rx, I build it on top of it. Follow-up There is a newer version of the ...
6
votes
1answer
331 views

Slices of Strings

In an attempt to summarize the question and all the good answers to this question, I decided to try to write a pair of string extensions that could split a string in respect to number of slices ...
5
votes
1answer
2k views

Extending IPrincipal.IsInRole()

In my case, I needed to give roles to users, but to specific departments, and I did not want to implement a new authorization mechanism which will require me to access the db everytime I want to check ...
3
votes
0answers
145 views

Extendable REST-Client - follow-up

My previous RestClient wasn't disposing a lot of resources. I've fixed it (hopefully without introducing new issues) and because a few new patterns emerged I did a ...
4
votes
1answer
938 views

Extendable REST-Client

I've been working on a REST-client. The client itself provides only very basic and generic functionality. Everything else is done with extensions. Its raw usage does not need to be convenient but ...
5
votes
3answers
2k views

LINQ method that selects an item based on previous one

my requirement is to implement a way to filter a collection of elements based on a value of previous one. For example to only select a number that is larger than the previous one. I don't currently ...
7
votes
2answers
8k views

Adding extension methods to IServiceCollection in ASP.NET Core

I have the following extension method to add themes support to my application: ...
3
votes
1answer
69 views

Loop + Take for repeatedly taking elements from the same collection

I sometimes need to enumerate the same collection multiple times so I wrote an extension that does this for me. I call it Loop. It takes one optional parameter <...
5
votes
2answers
192 views

Lag Lead Enumerator

I had a need to do a look-ahead and look-behind when enumerating though and IEnumerable. Here is a classic situation: ...
2
votes
1answer
69 views

Getting single element or throwing one of two exceptions

Sometimes I need to get a single thing from a collection but I'm not very happy with the Single extension. It does not differentiate between empty and more than one ...
2
votes
1answer
64 views

Very simplistic style add or update

In my library for styling the Console I need to be able to add or update two styles: color and ...
-1
votes
1answer
2k views

Retry cancelled tasks

I wrote an extension method for retrying tasks when cancelled. Can speed, versatility, readability, or elegance be improved at all? ...
1
vote
3answers
207 views
2
votes
1answer
203 views

Unit-test helper extensions for binary operators

This time I'd like you to review my unit-test helpers for binary operators. I wrote them because I'm often to lazy to test these operators because writing those tests is so boring. My little helpers ...
3
votes
2answers
3k views

Generic extension method for NullIf<T>() supporting nullable and non-nullable value types

I would like a simple, modestly efficient NullIf() generic extension for nullable and non-nullable value types including enums. The trickiness seems to be with ...
1
vote
1answer
2k views

Resizing an `IList<T>`

I need to resize an IList<T>. I have written two versions and would like to have a review of those. I am particularly interested on which of the two is to be ...