class String contains very useful method - String.Join(string, string[]).
It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with "<br />" or Environment.NewLine.
So I want to add an empty r...
I'm writing a .NET assembly in C++/CLI to be used in our C#-based application. I'd like the application to see some of the C++ methods as extension methods. Is there some attribute I can apply to the declaration to specify that a method should be seen as an extension method from C#?
...
Hi,
i need to create a HtmlHelperExtension in VB instead of C#. I cannot find any example showing how it's done. Since static classes don't exist in VB (modules are used instead), I'm not really sure on how to create a HtmlHelperExtension...
This is what I figured out, but it doesn't seem to work...
Public Module HtmlHelperExtensions
...
I have an Array<string>. I have to take all elements from i to j. How can I make this using an extension method?
...
Extension methods can be assigned to delegates that match their usage on an object, like this:
static class FunnyExtension {
public static string Double(this string str) { return str + str; }
public static int Double(this int num) { return num + num; }
}
Func<string> aaMaker = "a".Double;
Func<string, string> doubler = FunnyExtensio...
I have 3 similar functions, that only change based on numeric type:
<Extension()> _
Public Function ToNullableShort(ByVal str As String) As Short?
Dim intReturn As Short
If Short.TryParse(str, intReturn) Then
Return intReturn
Else
Return Nothing
End If
End Function
<Extension()> _
Public Function ToNulla...
If I have access only keys from a Dictionary<TKey, TValue> what is better to use:
Dictionary<TKey, TValue>.ForEach(pair => action(pair.Key))
or
Dictionary<TKey, TValue>.Keys.ForEach(key => action(key))
Which method is more 'best-practice' ? Speed in both cases I think seems to be very similar.
...
Hi,
I have created a Generic Extension method for DataRow object. The method takes no argument. I want to Invoke the Generic method through Reflection using MethodInfo. I can do this for Normarl public methods, but somehow I cannot get the reference of the Generic Extension method.
I've read this question on SO which somehwat relates t...
What naming conventions are you using for namespaces and sponsor classes? (i.e. the classes that hold extension method definitions)
Is there a standard/recommended .NET Framework naming convention? (the "Framework Design Guidelines, 2nd Edition" book only gives guidance on what namespaces not to use).
...
So I have an item class as follows:
class Item
{
private $db;
private $data = array(
'AltItem1' => null,
'AltItem2' => null,
'BaseUOM' => null,
'Category1' => null,
'Category2' => null,
'Category3' => null,
'Category4' => null,
'Iden' => null,
'IsHCS' => null,
'ItemDesc' => nul...
I'm going through the NerDinner free tutorial
http://nerddinnerbook.s3.amazonaws.com/Intro.htm
I got to somewhere in Step 5 where it says to make the code cleaner we can create an extension method. I look at the completed code and it has this to use the extension method:
catch {
ModelState.AddModelErrors(dinner.Get...
Here's my extension method for invoke on a control:
public static void Invoke<T>(this T c, Action<System.Windows.Forms.Control> DoWhat)
where T:System.Windows.Forms.Control
{
if (c.InvokeRequired)
c.Invoke(o=> DoWhat(c) );
else
DoWhat(c);
}
ds is a strongly typed dataset.
This works:
Action<DataGridView> a = row => row...
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,...
What is the general thinking on the use of extension methods that serve no purpose other than enhancing readability?
Without using extension methods we might have the method
IEnumerable<DependencyObject> GetDescendents(DependencyObject root) {}
that could be called with
var descendents = GetDescendents(someControl);
or
foreach (v...
I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but when I try to use the method in my view I get the error
'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'.
Can anyone tell me where I have gone wrong?
Helper module:
Imports System.Runtime...
I was wanting to output an integer to roman numerals and ran across this answer by Jesse Slicer. It is an extension method, but I was wondering about taking advantage of ToString(string, IFormatProvider) to do something like
int a = 10;
string b = a.ToString("RN", provider);
// OR
string c = string.Format(provider, "{0:RN} blah foo", a...
How do I access an extension method in an ASP.Net MVC View? In C# I do
using MyProject.Extensions;
and I remember seeing an XML equivalent to put in a view, but I can't find it anymore.
...
I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my...
I have some LINQ code that generates a list of strings, like this:
var data = from a in someOtherList
orderby a
select FunctionThatReturnsString(a);
How do I convert that list of strings into one big concatenated string? Let's say that data has these entries:
"Some "
"resulting "
"data here."
I should end up w...
Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times?
Thanks!
...