Hi
Is there a way to create an extension method for an type ? I only seem to be able to create them for instances.
public static class MyExtensions
{
public static string Test(this string s)
{
return "test";
}
}
public class Test
{
static void TestIt()
{
string.Test(); // won't compile
str...
Inspired by Javascripts variable Arguments in Max()/Min() and list comprehension in functional languages I tried to get the same in VB.NET using Generic Extension methods given IEnumerable(of T) as resulttype. This works well excepts for strings. Why?
These kind of extension methods may be considered a bad idea. Any strong reason Why t...
I'm trying to create the extension method AddRange for HashSet so I can do something like this:
var list = new List<Item>{ new Item(), new Item(), new Item() };
var hashset = new HashSet<Item>();
hashset.AddRange(list);
This is what I have so far:
public static void AddRange<T>(this ICollection<T> collection, List<T> list)
{
fore...
Ok. This is probably a really stupid question, but I'm going to ask it anyway...
How can I use extensions and utility methods in my ASP.Net markup? For example, (say) I have a DateTime extension method called "ToExampleString()" (contained in the DateTimeExtensions class in my Common.Extensions project) and I want to use it in my markup...
HashSet does not have an AddRange method, so I want to write an extension method for it. This is what I have:
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list)
{
foreach (var item in list)
{
collection.Add(item);
}
}
I have a base class, Media, and a derived class, Photo. This is t...
I'd like to override the Serialize methods of the ASP.NET JavaScriptSerializer class. Nothing too fancy, I just want to do some additional post processing to the serialized string returned from .NET.
Unfortunately, none of the methods on this class are declared virtual and the class itself does not derive from an interface or abstract ...
My colleague is extremely 'hot' on properly formatted and indented html being delivered to the client browser. This is so that the page source is easily readable by a human.
Firstly, if I have a partial view that is used in a number of different areas in my site, should the rendering engine be automatically formatting the indentations f...
I can understand the string.Join( )
var stringies1 =new [] {"Blood", "is", "Thicker", "Than", "Water" };
var zoin = string.Join("|", stringies1);
How does it differ from extension method Join() ?
I mean stringies1.Join(IEnumerable<T Result......)
...
In an attempt to clean up a lot of repeated code, I tried implementing the extension method below:
public static void AddIfNotPresent(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
}
}
public static ...
I've written dozens of extension methods and they all work as expected. But this is the first time I ran into using an extension method in this context.
public static class ControllerExtensions
{
public static RedirectToRouteResult RedirectToAction<TController>(
this Controller controller
, Expression<Action<TContro...
Hi
Is there a way to add new methods to a class, without modifying original class definition (i.e. compiled .lib containing class and corresponding .h file) like C#'s class extension methods?
...
So I've been dealing with several APIs recently provided by different software vendors for their products. Sometimes things are lacking, sometimes I just want to make the code more readable, and I'm trying to avoid a ton of static methods where they don't belong to "get what I need" from the APIs. Thus, I've found myself writing quite a ...
Ok,
We have a lot of where clauses in our code. We have just as many ways to generate a string to represent the in condition. I am trying to come up with a clean way as follows:
public static string Join<T>(this IEnumerable<T> items, string separator)
{
var strings = from item in items select item.ToString();
return string.Join...
I have written an extension method for string manipulation. I'm confused what should I name it - since this will become part of the base library front-end developers in the team will use. Here's the profile of the class member.
Info: Utility Extension method for String types. Overloads of this method may do the same thing characters oth...
Greetings!
I am using a WCF library on an application server, which is referenced by an IIS server (which is therefore the client). I would like to put my validation in a place so that I can just call .Validate() which returns a string array of errors (field too short, missing, etc). The problem is, such functions don't cross the WCF ...
Hi,
In what circumstances (usage scenarios) would you choose to write an extension rather than sub-classing an object ?
< full disclosure : I am not an MS employee; I do not know Mitsu Furota personally; I do know the author of the open-source Componax library mentioned here, but I have no business dealings with him whatsoever; I am no...
Hi,
I was wondering whether or not I can extend the Enum type in C# to implement my custom Enum.GetValues(type) and call it like Enum.GetMyCustomValues(type)
I am trying to implement something like this:
public static bool IsFlagSet<T>(this T value, T flag) where T : Enum
{
return (value & flag) != (T)0;
}
but it cannot be done....
If the LINQ Count() extension method is invoked on an IEnumerable<T> that has a Count property (e.g. List<T>), does the Count() method look for that property and return it (rather than counting the items by enumerating them)? The following test code seems to indicate that it does:
using System;
using System.Collections;
using System.Col...
Hi,
the source is throwing the error:
'nn.asdf' does not contain a definition for 'extension_testmethod'
and i really don't unterstand why...
using System.Linq;
using System.Text;
using System;
namespace nn
{
public class asdf
{
public void testmethod()
{
}
}
}
namespace nn_extension
{
using...
While looking at this question and it's answers I thought that it would be a good idea to write an extension method for System.Console that contained the desired functionality.
However, when I tried it, I got this compiler error
System.Console': static types cannot
be used as parameters
Here's the code:
using System;
using Sys...