extension-methods

How to convert type int[] to int?[]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 02:54:44
I'm using a linq query to output an int array. But I need to pass this into a method that only accepts int?[]. So after searching on ways to convert int[] to int?[] I found something that seemed might work here The following code is a simplified example that shows what is working and not working. using System; using System.Collections.Generic; using System.Web; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { // working... int[] vids1 = new[] { "", "1", "2", "3" } .Where(x => !String.IsNullOrWhiteSpace(x)) .Select(x => Convert.ToInt32(x)) .ToArray();

Controlling Output Indentation in ASP.Net MVC

无人久伴 提交于 2019-12-02 02:52:39
问题 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 for me (ala setting the Formatting property on an XmlTextWriter)? Secondly, my colleague has created a number of HtmlHelper extension methods for writing to the

Controlling Output Indentation in ASP.Net MVC

孤者浪人 提交于 2019-12-02 02:23:05
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 for me (ala setting the Formatting property on an XmlTextWriter)? Secondly, my colleague has created a number of HtmlHelper extension methods for writing to the response. These all require a CurrentIndent parameter to be passed to them. This smells wrong to me.

Can I add an enum to an existing .NET Structure, like Date?

可紊 提交于 2019-12-01 22:39:30
问题 So apparently Microsoft does not have a Months Enum on their Date structure. What I am wondering is, is it possible to create an enum and attach it to the DateTime structure? Extension Methods come instantly to mind, but I don't know of a way to pull this off using them. Dim july As DateTime.Months = DateTime.Months.July Public Enum Months January = 1 February = 2 March = 3 April = 4 May = 5 June = 6 July = 7 August = 8 September = 9 October = 10 November = 11 December = 12 End Enum Any one

Can I add an enum to an existing .NET Structure, like Date?

做~自己de王妃 提交于 2019-12-01 21:03:06
So apparently Microsoft does not have a Months Enum on their Date structure. What I am wondering is, is it possible to create an enum and attach it to the DateTime structure? Extension Methods come instantly to mind, but I don't know of a way to pull this off using them. Dim july As DateTime.Months = DateTime.Months.July Public Enum Months January = 1 February = 2 March = 3 April = 4 May = 5 June = 6 July = 7 August = 8 September = 9 October = 10 November = 11 December = 12 End Enum Any one have any thoughts on this? Update: I am not trying to get the Month name of the current Month or of a

Maximum number of occurrences a character appears in an array of strings

时间秒杀一切 提交于 2019-12-01 20:47:12
问题 In C#, given the array : string[] myStrings = new string[] { "test#test", "##test", "######", // Winner (outputs 6) }; How can I find the maximum number of occurrences that the character # appears in a single string ? My current solution is : int maxOccurrences = 0; foreach (var myString in myStrings) { var occurrences = myString.Count(x => x == '#'); if (occurrences > maxOccurrences) { maxOccurrences = occurrences; } } return maxOccurrences; Is their a simplier way using linq that can act

Maximum number of occurrences a character appears in an array of strings

守給你的承諾、 提交于 2019-12-01 18:48:55
In C#, given the array : string[] myStrings = new string[] { "test#test", "##test", "######", // Winner (outputs 6) }; How can I find the maximum number of occurrences that the character # appears in a single string ? My current solution is : int maxOccurrences = 0; foreach (var myString in myStrings) { var occurrences = myString.Count(x => x == '#'); if (occurrences > maxOccurrences) { maxOccurrences = occurrences; } } return maxOccurrences; Is their a simplier way using linq that can act directly on the myStrings[] array ? And can this be made into an extension method that can work on any

Extension methods overloading in C#, does it work?

点点圈 提交于 2019-12-01 17:24:25
Having a class that has a method, like this: class Window { public void Display(Button button) { // ... } } is it possible to overload the method with another one that is more broad, like this: class WindowExtensions { public void Display(this Window window, object o) { Button button = BlahBlah(o); window.Display(button); } } What happened when I tried is that I have infinite recursion. Is there a way to make that work? I want the extension method to be called only when the other method can't be called. Let's go to the specification. First, we have to understand the rules for method

C# extension method for a method group

送分小仙女□ 提交于 2019-12-01 17:19:29
I want to implement an extension method for a method. Consider the following code sample ( http://dotnetfiddle.net/HztiOo ) : using System; using System.Collections.Generic; public class Program { public static void Main() { A a = new A(); // Noticed that Next() is called twice Console.WriteLine(a.Next(1)); Console.WriteLine(a.Next(1)); // Works var withCache = ((Func<int,int>)a.Next).AddCaching(); withCache = new Func<int,int>(a.Next).AddCaching(); withCache = ExtensionMethods.AddCaching<int,int>(a.Next); // Doesn't work :( // withCache = a.Next.AddCaching<int,int>(); // Func<int,int>

Can you make an Extension Method Static/Shared?

六眼飞鱼酱① 提交于 2019-12-01 16:58:44
OK, I've probably misunderstood something here but, as far as I can see ... An extension method has to be contained in a module, not a class You can't make methods in modules Static/Shared Therefore you can't use an extension method on a class without instantiating it. In other words you can't make an extension method on String called "MyExtensionMethod" and use: String.MyExtensionMethod("String") But instead .. Dim test As String test.MyExtensionMethod("string") Is this correct? Or is there a way I can get extension methods to work as static methods? You are correct. Extension methods can