I'm working on an extension method that's only applicable to reference types. I think, however, it's currently boxing and unboxing the the value. How can I avoid this?
namespace System
{
public static class SystemExtensions
{
public static TResult GetOrDefaultIfNull<T, TResult>(this T obj, Func<T, TResult> getValue, TR...
Hi all,
(LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0);
this name value is coming from database..
what happening here is if this name value is null while reading it's throwing an exception?
I am manually doing some if condition here. I don't want to write a manual condition to check all my variables..
I am doi...
I'd like to create a lot of extension methods for some generic class, e.g. for
public class SimpleLinkedList<T> where T:IComparable
And I've started creating methods like this:
public static class LinkedListExtensions
{
public static T[] ToArray<T>(this SimpleLinkedList<T> simpleLinkedList) where T:IComparable
{
//// c...
Why is it forbidden to call Extension method with ref modifier?
This one is possible:
public static void Change(ref TestClass testClass, TestClass testClass2)
{
testClass = testClass2;
}
And this one not:
public static void ChangeWithExtensionMethod(this ref TestClass testClass, TestClass testClass2)
{
te...
What I'm trying to do is create an extension method for the HtmlHelper to create a specific output and associated details like TextBoxFor<>. What I want to do is specify the property from the model class as per TextBoxFor<>, then an associated controller action and other parameters.
So far the signature of the method looks like:
public...
I have the following methods in an enum helper class (I have simplified it for the purpose of the question):
static class EnumHelper
{
public enum EnumType1 : int
{
Unknown = 0,
Yes = 1,
No = 2
}
public enum EnumType2 : int
{
Unknown = 0,
Dog = 1,
Cat = 2,
Bird...
I've implemented some extension methods and put those in separate Class Library project.
Imagine I have a simple extension method like this in class library called MD.Utility:
namespace MD.Utility
{
public static class ExtenMethods
{
public static bool IsValidEmailAddress(this string s)
{
Regex regex...
Hi,
I've created an extension method for SqlCommand that allows some additional work before executing the command:
public static SqlDataReader ExecuteMyReader( this SqlCommand sqlCommand )
{
// Some calculations here
return sqlCommand.ExecuteReader();
}
My question is what is the best way to unit test this extension method?
...
I can't seem to get the following extension method to be found in another class in the same namespace (MyProject.Util).
using System.Collections.Specialized;
namespace MyProject.Util
{
public static class Extensions
{
public static string Get(
this NameValueCollection me,
string key,
...
I have seen plenty of online guidelines for authoring extension methods, usually along these lines:
1) Avoid authoring extension methods when practical - prefer other approaches first (e.g. regular static methods).
2) Don't author extension methods to extend code you own or currently develop. Instead, author them to extend 3rd party o...
I am trying to call the RenderAction Extension Method within my own Html Helper:
System.Web.Mvc.Html.ChildActionExtensions.RenderAction(helper, "account", "login");
this is so that along with some additional logic, I would like all html helpers to use a common method name structure when calling it on the view:
<%= Html.CompanyName()....
Hi, got question regarding an extension method that I have written that looks like this:
public static IEnumerable<T> FindControlsOfType<T>(this ControlCollection instance) where T : class
{
T control;
foreach (Control ctrl in instance)
{
if ((control = ctrl as T) != null)
{
yield return control...
I can have an extension method like this:
DateTime d = new DateTime();
d = d.GetRandomDate();
GetRandomDate is my extension method. However the above doesn't make much sense. What would be better is:
DateTime d = DateTime.GetRandomDate();
However, I don't know how to do this. An extension method created as:
public static DateTime ...
I typically name my C# interfaces as IThing. I'm creating an extension method class for IThing, but I don't know what to name it. On one hand, calling it ThingExtensions seems to imply it is an extension class to some Thing class instead of to the IThing interface. It also makes the extension class be sorted away from the interface it...
I've been trying to make my way through this article:
http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx
... And something on page 1 made me uncomfortable. In particular, I was trying to wrap my head around the Compose<>() function, and I wrote an example for myself. Consider the following two Func's:
Func<d...
Is there a way to override extension methods (provide a better implementation), without explicitly having to cast to them? I'm implementing a data type that is able to handle certain operations more efficiently than the default extension methods, but I'd like to keep the generality of IEnumerable. That way any IEnumerable can be passed, ...
Hi.
Extending methods to any instance is really easy:
public static string LeaveJustNumbers(this string text)
{
return Regex.Replace(text, @"[\D]", "");
}
...
string JustNumbers = "A5gfb343j4".LeaveJustNumber();
But what if i want to extend methods to a sealed class like string, to
work like:
string.Format("Hi:{0}","Fraga");
I...
I'm very new to Ruby (and OOP as well) and I don't know why the following thing doesn't work in Ruby
I extended the String class with a new method. Easy enough. Now I want to extend the Fixnum class. A String object appears somewhere in the class, but I can't use the method that I defined earlier. Why? Is this normal?
This is the new m...
My web application deals with strings that need to be converted to numbers alot - users often put commas, units (like cm, m, g, kg) and currency symbols in these fields so what I want to do is create a string extension method that cleans the field up and converts it to a decimal.
For example:
decimal myNumber = "15 cm".ToDecimal();
...
Hi,
I would like to develop a library that I can re-use to add various methods involved in navigating/searching through a graph (nodes/relationships, or if you like vertexs/edges). The generic requirements would be:
There are existing classes in the main project that already implement the equivalent of the graph class (which contains...