Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

-1
votes
1answer
57 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 ...
1
vote
2answers
90 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
59 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
39 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
226 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 ...
4
votes
1answer
52 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
61 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
64 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
94 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
329 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
329 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 ...
7
votes
2answers
265 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
222 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 ...
9
votes
4answers
556 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
158 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 ...
5
votes
2answers
311 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
419 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
235 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
1k 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
126 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
738 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 ...
4
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
67 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
1k views

Retry cancelled tasks

I wrote an extension method for retrying tasks when cancelled. Can speed, versatility, readability, or elegance be improved at all? ...
2
votes
1answer
134 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
2k 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
336 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
138 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....
10
votes
3answers
6k 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
215 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
265 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 ...
2
votes
1answer
171 views

Mitsubishi logo builder

I have created a Mitsubishi logo builder that takes input from the logo.txt file and uses those ints/chars to create however many logos that are identified in the logo.txt file. I just feel that my ...
2
votes
1answer
276 views

regex string extension

I wrote some string-extensions: ...
4
votes
1answer
2k views

Checking if string contains other strings

This is simply an extension method which checks if a string str contains all the values in values. However, there are cases ...
2
votes
3answers
3k views

Methods to create C# dictionaries, with static keys/values, in fluent notation

I created these little utility methods to build fluent C# dictionaries. The value declarations shall be quick to write and easy to comprehend when somebody views the code. Usage conditions are as ...
0
votes
1answer
155 views

Scala implicit conversion to add methods to Int

I started learning Scala, and like most functional languages, things get messy pretty quickly for a beginner. Here I have built a wrapper class for Ints so that I ...
4
votes
4answers
3k views

Extension method to retrieve list of days in the current week

I want to get all days of the current week (from Monday to Sunday) with a custom time. To do this, I have written an extension method for TimeSpan: ...
3
votes
2answers
444 views

Validation Extension method performance

I'm writing a simple set/framework of extension methods for argument/parameter validation. What I'm mostly concerned about, ATM is the performance, against the simpliest boilerplate ...
1
vote
2answers
3k views

Find substring of a string in Swift 3

I am using this basic extension for a Swift project. It works well, but I want to make sure it is the absolute fastest way to get a substring. ...