extension-methods

How do I extend a class with c# extension methods?

只谈情不闲聊 提交于 2019-12-17 08:16:12
问题 Can extension methods be applied to the class? For example, extend DateTime to include a Tomorrow() method that could be invoked like: DateTime.Tomorrow(); I know I can use static DateTime Tomorrow(this Datetime value) { //... } Or public static MyClass { public static Tomorrow() { //... } } for a similar result, but how can I extend DateTime so that I could invoke DateTime.Tomorrow? 回答1: You cannot add methods to an existing type unless the existing type is marked as partial, you can only

Why doesn't VS 2008 display extension methods in Intellisense for String class

不羁岁月 提交于 2019-12-17 07:47:58
问题 Since String implements IEnumerable<char> , I was expecting to see the Enumerable extension methods in Intellisense, for example, when typing the period in String s = "asdf"; s. I was expecting to see .Select<char>(...) , .ToList<char>() , etc. I was then suprised to see that the extension methods do in fact work on the string class, they just don't show up in Intellisense. Does anyone know why this is? This may be related to this question. 回答1: It's by explicit design. The problem is that

FindAll vs Where extension-method

╄→尐↘猪︶ㄣ 提交于 2019-12-17 07:21:36
问题 I just want know if a "FindAll" will be faster than a "Where" extentionMethod and why? Example : myList.FindAll(item=> item.category == 5); or myList.Where(item=> item.category == 5); Which is better ? 回答1: Well, FindAll copies the matching elements to a new list, whereas Where just returns a lazily evaluated sequence - no copying is required. I'd therefore expect Where to be slightly faster than FindAll even when the resulting sequence is fully evaluated - and of course the lazy evaluation

Using extension methods in .NET 2.0?

随声附和 提交于 2019-12-17 07:21:28
问题 I want to do this, but getting this error: Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff] I have seen some answers here that says, you have to define this attribute yourself. How do I do that? EDIT : This is what I have: [AttributeUsage ( AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method )]

How do extension methods work?

北城余情 提交于 2019-12-17 03:44:11
问题 I want to make an extension method in Unity3d for the Vector3 class. But I don't quite seem to get it. This is what I have: public static class ExtensionMethods{ public static Vector3 MaxValue(this Vector3 _vec3) { return new Vector3(float.MaxValue,float.MaxValue,float.MaxValue); } } Now I want to make a Vector3.MaxValue just like float.MaxValue with this line of code: Vector3 closestPoint = Vector3.MaxValue; But then I get this error: error CS0117: `UnityEngine.Vector3' does not contain a

Convert string to nullable type (int, double, etc…)

ε祈祈猫儿з 提交于 2019-12-17 03:23:23
问题 I am attempting to do some data conversion. Unfortunately, much of the data is in strings, where it should be int's or double, etc... So what I've got is something like: double? amount = Convert.ToDouble(strAmount); The problem with this approach is if strAmount is empty, if it's empty I want it to amount to be null, so when I add it into the database the column will be null. So I ended up writing this: double? amount = null; if(strAmount.Trim().Length>0) { amount = Convert.ToDouble(strAmount

ArgumentNullException or NullReferenceException from extension method?

坚强是说给别人听的谎言 提交于 2019-12-17 02:31:52
问题 What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they're used like instance methods so it might be more natural to use the NullReferenceException. Let's take the following example: public static string ToInvariantString(this IFormattable value,

Detect target framework version at compile time

眉间皱痕 提交于 2019-12-17 01:40:03
问题 I have some code which makes use of Extension Methods, but compiles under .NET 2.0 using the compiler in VS2008. To facilitate this, I had to declare ExtensionAttribute: /// <summary> /// ExtensionAttribute is required to define extension methods under .NET 2.0 /// </summary> public sealed class ExtensionAttribute : Attribute { } However, I'd now like the library in which that class is contained to also be compilable under .NET 3.0, 3.5 and 4.0 - without the 'ExtensionAttribute is defined in

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

浪子不回头ぞ 提交于 2019-12-16 19:40:28
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Let's make a list of answers where you post your excellent and favorite extension methods. The requirement is that the full code must be posted and a example and an explanation on how to use it. Based on the high interest in this topic I have setup an Open Source Project called extensionoverflow on Codeplex. Please mark

Extension Methods for IEnumerable<Enum>?

余生长醉 提交于 2019-12-14 04:25:17
问题 I have a bunch of different enums, such as... public enum MyEnum { [Description("Army of One")] one, [Description("Dynamic Duo")] two, [Description("Three Amigo's")] three, [Description("Fantastic Four")] four, [Description("The Jackson Five")] five } I wrote an extension method for any Enum to get the Description attribute if it has one. Simple enough right... public static string GetDescription(this Enum currentEnum) { var fi = currentEnum.GetType().GetField(currentEnum.ToString()); var da