extension-methods

How do I properly write Math extension methods for int, double, float, etc.?

荒凉一梦 提交于 2019-12-09 13:09:44
问题 I want to write a series of Extension methods to simplify math operations. For example: Instead of Math.Pow(2, 5) I'd like to be able to write 2.Power(5) which is (in my mind) clearer. The problem is: how do I deal with the different numeric types when writing Extension Methods? Do I need to write an Extension Method for each type: public static double Power(this double number, double power) { return Math.Pow(number, power); } public static double Power(this int number, double power) { return

Fastest way to convert a list of strings into a single concatenated string?

微笑、不失礼 提交于 2019-12-09 10:03:43
问题 I have some LINQ code that generates a list of strings, like this: var data = from a in someOtherList orderby a select FunctionThatReturnsString(a); How do I convert that list of strings into one big concatenated string? Let's say that data has these entries: "Some " "resulting " "data here." I should end up with one string that looks like this: "Some resulting data here." How can I do this quickly? I thought about this: StringBuilder sb = new StringBuilder(); data.ToList().ForEach(s => sb

Decouple EF queries from BL - Extension Methods VS Class-Per-Query

倖福魔咒の 提交于 2019-12-09 04:24:53
问题 I have read dozens of posts about PROs and CONs of trying to mock \ fake EF in the business logic. I have not yet decided what to do - but one thing I know is - I have to separate the queries from the business logic. In this post I saw that Ladislav has answered that there are 2 good ways: Let them be where they are and use custom extension methods, query views, mapped database views or custom defining queries to define reusable parts. Expose every single query as method on some separate

What is the best way to extend null check?

空扰寡人 提交于 2019-12-09 04:20:16
问题 You all do this: public void Proc(object parameter) { if (parameter == null) throw new ArgumentNullException("parameter"); // Main code. } Jon Skeet once mentioned that he sometimes uses the extension to do this check so you can do just: parameter.ThrowIfNull("parameter"); So I come of with two implementations of this extension and I don't know which one is the best. First: internal static void ThrowIfNull<T>(this T o, string paramName) where T : class { if (o == null) throw new

How do I override ToString() and implement generic?

会有一股神秘感。 提交于 2019-12-09 03:24:38
问题 I have code that I want to make the following changes: How do I override ToString()? It says: A static member ...ToString(System.Collections.Generic.List)' cannot be marked as override, virtual, or abstract. How do I make it generic? public static override string ToString(this List<int> list) { string output = ""; list.ForEach(item => output += item.ToString() + "," ); return output; } Thanks! 回答1: What are you trying to achieve? Often I want to output the contents of a list, so I created the

Can There Be Private Extension Methods?

僤鯓⒐⒋嵵緔 提交于 2019-12-09 02:24:57
问题 Let's say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that helper to the only class that actually needs to use it? For example, I try this: class Program { static void Main(string[] args) { var value = 0; value = value.GetNext(); // Compiler error } static int GetNext(this int i) { return i + 1; } } The compiler doesn't "see" the GetNext() extension method. The error is: Extension

Correctly making an ActionLink extension with htmlAttributes

十年热恋 提交于 2019-12-09 01:52:32
问题 I use a custom extension for my ActionLinks. I have added an attribute data_url which is meant to be translated to an attribute of data-url . This is, replacing the underscaore with a dash. Here is link 1 using my custom extension: @Ajax.ActionLink("Add", MyRoutes.GetAdd(), new AjaxOptions() , new { data_url = Url.Action(...)}) Result: data_url Here is link 2 using the framework ActionLink: @Ajax.ActionLink("Add 2", "x", "x", null, new AjaxOptions() , new { data_url = Url.Action(...) })

What does “this” mean when used as a prefix for method parameters?

不打扰是莪最后的温柔 提交于 2019-12-08 22:11:48
问题 I'm sure the answer is something obvious, and I'm kind of embarrassed that I don't really know the answer already, but consider the following code sample I picked up while reading "Professional ASP.NET MVC 1.0": public static class ControllerHelpers { public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors) { foreach (RuleViolation issue in errors) modelState.AddModelError(issue.PropertyName, issue.ErrorMessage); } } I understand what this

.NET List.Distinct

折月煮酒 提交于 2019-12-08 16:39:30
问题 I'm using .NET 3.5. Why am I still be getting: does not contain a definition for 'Distinct' with this code: using System.Collections.Generic; //.. . . . . code List<string> Words = new List<string>(); // many strings added here . . . Words = Words.Distinct().ToList(); 回答1: Are you using System.Linq; ? Distinct is an extension method defined in System.Linq.Enumerable so you need to add that using statement. And don't forget to add a reference to System.Core.dll (if you're using VS2008, this

Error 'Iterator cannot contain return statement ' when calling a method that returns using a yield

拟墨画扇 提交于 2019-12-08 14:58:05
问题 I'm hoping there's a nicer way to write this method & overloads with less code duplication. I want to return a sequence of deltas between items in a list. this method:- public static IEnumerable<decimal> CalculateDeltas(this IEnumerable<decimal> sequence) { decimal prev = default(decimal); foreach (var item in sequence) { var current = item; decimal diff = current - prev; prev = item; yield return diff; } } works just fine. I then thought about an overload which would allow an absolute delta,