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.

6
votes
3answers
231 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
189 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
162 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
142 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 ...
0
votes
0answers
30 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
22 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
64 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
62 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
114 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
64 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
284 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
57 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
62 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
70 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
100 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
418 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
405 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
312 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
2k 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
271 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
603 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
159 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
418 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
481 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 ...
4
votes
0answers
272 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
133 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
854 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
7k 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 <...
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 ...
1
vote
1answer
58 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
194 views
2
votes
1answer
164 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
1k 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 ...
2
votes
2answers
3k views

Enum with associated value as stored property in Swift extension

I have a service which connects to server using socket and stays connected, I used this enum to manage connection state: ...
2
votes
1answer
355 views

Storing realm objects using Kotlin extensions

I am a Java Android Developer and I'm approaching Kotlin. I have defined the following class: open class Player : RealmObject() { ... } And I defined the ...
2
votes
1answer
2k views

Int-Enum to String and vice a versa in Swift

I have a situation in my client-server app where I have to send my network strength to server and also receive other clients' strength from server. So I created enum as: ...
2
votes
1answer
140 views

Extension method replacing elements from an IEnumerable<T> collection follow up

I got some amazing feedback on my previous question and I decided to extend this method and add it to my own library where every now and than I will add some methods I feel can be useful in the future....
11
votes
3answers
7k views

Extension method replacing elements from an IEnumerable<T> collection

Today I needed a method to replace all 'x' values in a said collection. Since there is no such method by default in .NET I wrote my own: ...
4
votes
1answer
164 views

Bidimensional array trimming extensions

After stumbling upon this question on StackOverflow, I started playing with some extension methods to trim null objects from a bi-dimensional array. This is what I'...
3
votes
3answers
92 views

Extension method to retrieve a sample from a collection

I wrote a little extension method to retrieve a random sample of items from a collection. I've tried to write clean code. What do you think? is it clean? does it work as you would expect? I'll be ...
5
votes
2answers
220 views

Helper method to shuffle cards list for online gaming

I have written this code to shuffle a deck of cards. I would like to hear your inputs. ...
0
votes
1answer
283 views

Merging union Observables

I am having a scenario where I need to execute observables that depends on the result of the first one. However I need to keep the result of the first observable. I couldn't find any extension that ...