extension-methods

Extension Method in C# 2.0

霸气de小男生 提交于 2019-11-26 21:47:59
问题 What namespace do I need to get my extension to work Here is my Extension Method using System; using System.Collections.Generic; using System.Web; using System.Data; namespace MyUtilities { public static class DataReaderExtensions { public static DataTable ToDataTable(IDataReader reader) { DataTable table = new DataTable(); table.Load(reader); return table; } } } When I try to use it in like this Session["Master"] = cust.GetCustomerOrderSummary((string)Session["CustomerNumber"]).ToDataTable()

Checking for nil Value in Swift Dictionary Extension

只愿长相守 提交于 2019-11-26 21:42:18
问题 I have the following code in a playground file: extension Dictionary { func test() { for key in self.keys { self[key] } } } var dict: [String: AnyObject?] = [ "test": nil ] dict.test() I will henceforth refer to the line within the for-each loop as the output since it is what's relevant. In this particular instance the output is nil . When I change the for-each loop to look like this: for key in self.keys { print(self[key]) } The output is "Optional(nil)\n" . What I really want to do is check

Swift extension example

拈花ヽ惹草 提交于 2019-11-26 21:19:46
I was originally wanting to know how to make something like this UIColor.myCustomGreen so that I could define my own colors and use them throughout my app. I had studied extensions before and I thought that I could probably use them to solve my problem, but I couldn't remember exactly how to set extensions up. Searching on Google at the time of this writing for "Swift extension" resulted in the documentation , several long tutorials , and a rather unhelpful Stack Overflow question . So the answers are out there, but it takes some digging through the docs and tutorials. I decided to write this

Why does this extension method throw a NullReferenceException in VB.NET?

三世轮回 提交于 2019-11-26 20:25:30
问题 From previous experience I had been under the impression that it's perfectly legal (though perhaps not advisable) to call extension methods on a null instance. So in C#, this code compiles and runs: // code in static class static bool IsNull(this object obj) { return obj == null; } // code elsewhere object x = null; bool exists = !x.IsNull(); However, I was just putting together a little suite of example code for the other members of my development team (we just upgraded to .NET 3.5 and I've

Extension methods syntax vs query syntax

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:42:56
I'm trying to get a handle on if there's a good time to use standard linq keywords or linq extension methods with lambda expressions. They seems to do the same thing, just are written differently. Is it purely a matter of style? var query = from p in Products where p.Name.Contains("foo") orderby c.Name select p; // or with extension methods: var query = Products .Where(p => p.Name.Contains("foo")) .OrderBy(p => p.Name); They're very similar with the second example being a bit more terse, but perhaps less expressive if you don't know what the => is doing. Other than writing terse code, are

Extending the C# Coalesce Operator

本秂侑毒 提交于 2019-11-26 19:31:49
问题 Before I explain what I want to do, if you look at the following code, would you understand what it's supposed to do? (updated - see below) Console.WriteLine( Coalesce.UntilNull(getSomeFoo(), f => f.Value) ?? "default value"); C# already has a null-coalescing operator that works quite well on simple objects but doesn't help if you need to access a member of that object. E.g. Console.WriteLine(getSomeString()??"default"); works very well but it won't help you here: public class Foo { public

Extension Methods vs Static Utility Class [closed]

故事扮演 提交于 2019-11-26 19:23:15
问题 I'm looking for some pros and cons for using extension methods over static utility classes in a C# app. For instance, a plus in the extension methods column is the convinience of calling by the class name rather than something like "StringUtils". But a con would be that it can blur the lines between what is in the framework and what isn't. 回答1: I would say that a pro is that it blurs the lines between what is in the framework and what isn't: you can use your own code as naturally as framework

Is there any way in C# to override a class method with an extension method?

孤街浪徒 提交于 2019-11-26 18:57:09
There have been occasions where I would want to override a method in a class with an extension method. Is there any way to do that in C#? For example: public static class StringExtension { public static int GetHashCode(this string inStr) { return MyHash(inStr); } } A case where I've wanted to do this is to be able to store a hash of a string into a database and have that same value be used by all the classes that use the string class's hash (i.e. Dictionary, etc.) Since the built-in .Net hashing algorithm is not guaranteed to be compatible from one version of the Framework to the next, I want

Code equivalent to the 'let' keyword in chained LINQ extension method calls

两盒软妹~` 提交于 2019-11-26 18:47:04
问题 Using the C# compilers query comprehension features, you can write code like: var names = new string[] { "Dog", "Cat", "Giraffe", "Monkey", "Tortoise" }; var result = from animalName in names let nameLength = animalName.Length where nameLength > 3 orderby nameLength select animalName; In the query expression above, the let keyword allows a value to be passed forward to the where and orderby operations without duplicate calls to animalName.Length . What is the equivalent set of LINQ extension

Static extension methods [duplicate]

放肆的年华 提交于 2019-11-26 18:39:46
Possible Duplicate: Can I add extension methods to an existing static class? Is there any way I can add a static extension method to a class. specifically I want to overload Boolean.Parse to allow an int argument. In short, no, you can't. Long answer, extension methods are just syntactic sugar. IE: If you have an extension method on string let's say: public static string SomeStringExtension(this string s) { //whatever.. } When you then call it: myString.SomeStringExtension(); The compiler just turns it into: ExtensionClass.SomeStringExtension(myString); So as you can see, there's no way to do