extension-methods

String.IsNullOrBlank Extension Method

余生颓废 提交于 2019-11-30 08:44:01
I continuously check string fields to check if they are null or blank. if(myString == null || myString.Trim().Length == 0) { throw new ArgumentException("Blank strings cannot be handled."); } To save myself a bit of typing is it possible to create an extension method for the String class that would have the same effect? I understand how extension methods can be added for a class instance but what about adding a static extension method to a class? if(String.IsNullOrBlank(myString)) { throw new ArgumentException("Blank strings cannot be handled."); } Sean You could do: public static bool

Is it possible to create constructor-extension-method ? how?

折月煮酒 提交于 2019-11-30 07:47:38
Is it possible to add a constructor extension method ? I want to add a List< T > constructor to receive specific amount of bytes out of a given partially filled buffer (without the overhead of copying only the relevant bytes and so on): ... public static List<T>(this List<T> l, T[] a, int n) { for (int i = 0; i < n; i++) l.Add(a[i]); } ... so the usage would be: List<byte> some_list = new List<byte>(my_byte_array,number_of_bytes); I've already added an AddRange extension method: public static void AddRange<T>(this List<T> l, T[] a, int n) { for (int i = 0; i < n; i++) l.Add(a[i]); } I want to

Best method to use IDataReader as IEnumerable<T>?

北战南征 提交于 2019-11-30 07:36:12
I need to use Linq on any IDataReader implementations like this var c = sqlDataReader.AsEnumerable().Count(); Example: public abstract class Test { public abstract SqlDataReader GetSqlDataReader(); public void Foo() { SqlDataReader sqlDataReader = GetSqlDataReader(); IEnumerable<SqlDataReader> sqlEnumerable = sqlDataReader.AsEnumerable(); var c = sqlEnumerable.Count(); var s = sqlEnumerable.Sum(); SqlDataReader first = sqlEnumerable.First(); var t = first.GetSqlXml(10); } } What is the best way to write this. Please, write your snippet. Try, this: public static class DataReaderExtension {

Is it possible to extend arrays in C#?

烂漫一生 提交于 2019-11-30 07:31:53
问题 I'm used to add methods to external classes like IEnumerable. But can we extend Arrays in C#? I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional. Not related to How to extend arrays in C# 回答1: static class Extension { public static string Extend(this Array array) { return "Yes, you can"; } } class Program { static void Main(string[] args) { int[,,,] multiDimArray = new int[10,10,10,10]; Console.WriteLine(multiDimArray.Extend()); } } 回答2:

Why must C# extension methods be defined in static classes? [duplicate]

风格不统一 提交于 2019-11-30 07:15:25
This question already has an answer here: Why are extension methods only allowed in non-nested, non-generic static class? 3 answers I understand that C# extension methods must be static. What I don't understand is why these extensions can't be defined in non static classes or generic ones? Update: I am interested in the reason behind this design decision. This is more of an observation than an answer, but... When you call an instance method, a reference to the object you are calling is pushed onto the stack as the first argument in your method call. That first argument is "this" and is done

Create Extension Method to Produce Open & Closing Tags like Html.BeginForm()

眉间皱痕 提交于 2019-11-30 07:09:14
I wonder if it's possible to create an extension method which has functionality & behaviour similar to Html.BeginForm(), in that it would generate a complete Html tag, and I could specificy its contents inside <% { & } %> tags. For example, I could have a view like: <% using(Html.BeginDiv("divId")) %> <% { %> <!-- Form content goes here --> <% } %> This capability would be very useful in the context of the functionality I'm trying to produce with the example in this question This would give me the ability to create containers for the types that I'll be <% var myType = new MyType(123, 234); %>

How can I create .Net extension methods by C++/CLI?

梦想的初衷 提交于 2019-11-30 06:41:30
In C#, extension methods can be created by public static class MyExtensions { public static ReturnType MyExt(this ExtType ext) { ... } } Since all of my library are written in C++/CLI, I would like to create the .net extension methods also in C++/CLI (in order to have one DLL instead of two). I've tried the following code static public ref class MyExtensions { public: static ReturnType^ MyExt(this ExtType ^ext) { ... } }; But the compiler can not recognize keyword 'this' in the first argument. error C2059: syntax error: 'this' Is there some way to create the extension method in C++/CLI ? You

Error: Extension methods must be defined in a top level static class (CS1109)

ε祈祈猫儿з 提交于 2019-11-30 06:02:38
问题 I'm trying to make a countdown program, which I can start and stop and set the value of the countdown to 10 minutes if needed. But I'm getting an error I don't quite understand. I'm not that into C#, so here's the code: Can some one help me a bit here ? Think I run on framework 3.0 or something ? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System

Why doesn't the Controls collection provide all of the IEnumerable methods?

橙三吉。 提交于 2019-11-30 05:39:29
I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me. I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault(); However from what I can tell, Controls does implement the IEnumerable interface which provides such methods, so what gives? Why doesn't that just work? I have found a decent work around for this issue at least: var list = (IEnumerable<Control>)Controls; var this_item = list.Where(x => x.ID ==

Existing LINQ extension method similar to Parallel.For? [duplicate]

荒凉一梦 提交于 2019-11-30 04:58:22
Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> The linq extension methods for ienumerable are very handy ... but not that useful if all you want to do is apply some computation to each item in the enumeration without returning anything. So I was wondering if perhaps I was just missing the right method, or if it truly doesn't exist as I'd rather use a built-in version if it's available ... but I haven't found one :-) I could have sworn there was a .ForEach method somewhere, but I have yet to find it. In the meantime, I did write my own version in case it's useful for anyone