Omg nifty.net is down, that's been my source for erotica for like 4 years man
seen from United States

seen from Germany

seen from United States
seen from United States

seen from United States
seen from Romania

seen from Singapore

seen from Germany

seen from Egypt
seen from United States
seen from United States

seen from Romania

seen from Singapore

seen from Egypt
seen from United States
seen from China

seen from Malaysia

seen from United States
seen from United States
seen from China
Omg nifty.net is down, that's been my source for erotica for like 4 years man
Nifty .NET Part #4: Enumerable.Any
In part 4 of the Nifty .Net series we have the Enumerable.Any method. The Any method is part of the .NET 3.5 LINQ framework, so it’s only available in .NET 3.5 and upwards. The Any method determines whether a sequence contains any elements.
The method definition:
public static bool Any(this IEnumerable source)
As you can see the the Any method is an extension method on IEnumerable, so you can use the Any method on al classes that implements IEnumerable, like List, string[].
For example:
using System; using System.Linq; public class MyClass { public static void Main() { string[] foo = { "a", "b", "c" }; if(foo.Any()) { Console.WriteLine("Contains any elements"); } Console.ReadLine(); } }
In this code snippet we determines if the foo array has any elements. To use the Any method we have to add the System.Linq namespace as “using” to our class.
Some of the great advantages of the Any method is that it will stop the enumeration of source as soon as the result can be determined.
Beware don’t use the method .Count() like the following code snippet:
using System; using System.Linq; public class MyClass { public static void Main() { string[] foo = { "a", "b", "c" }; if(foo.Count() > 0) { Console.WriteLine("Contains any elements"); } Console.ReadLine(); } }
The above code snippet uses a lot more resources to determine if a sequence contains any elements, because it have to enumerate through all elements of the collection to get the count. The Any method, as I said earlier, stops as soon as he found an element in the collection.
The Any method has also an overload:
public static bool Any(this IEnumerable source, Func<bool> predicate)
With this overload you can check if there are any elements in collection that matches the predicate. A code example will look like this:
using System; using System.Linq; public class MyClass { public static void Main() { string[] foo = { "a", "b", "c" }; if(foo.Any(s => s == "a")) { Console.WriteLine("Contains any elements"); } Console.ReadLine(); } }
This code example will look for any elements that matches with “a”, and again it will stops if as soon as the result can be determined.
In some cases you can beter use “.length > 0”, this property has a better performance compared to the Any method:
using System; public class MyClass { public static void Main() { string[] foo = { "a", "b", "c" }; if(foo.Length > 0) { Console.WriteLine("Contains any elements"); } Console.ReadLine(); } }
The snippet above is faster because the length property is updated when an item is added or deleted from the list, so it doesn’t have to enumerate through the list.
You can find more info and examples on the MSDN pages:
Enumerable.Any(IEnumerable)
Enumerable.Any(IEnumerable, Func)
Nifty .NET Part #3: String.PadLeft
And there is part 3, String.PadLeft, this method will add a specified character to the left side of string till the specified length is met.
The method definition:
public string PadLeft(int totalWidth, char paddingChar) .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
So what can you to with the PadLeft method in practice? The following code sample will add leading zero’s only when it’s needed:
1: using System;
2:
3: public class MyClass
4: {
5: public static void Main()
6: {
7: // Displays: 001
8: Console.WriteLine("1".PadLeft(3, '0'));
9:
10: // Displays: 010
11: Console.WriteLine("10".PadLeft(3, '0'));
12:
13: // Displays: 100
14: Console.WriteLine("100".PadLeft(3, '0'));
15:
16: Console.ReadLine();
17: }
18: }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
As you can see if the string length is already equal to the total width of the PadLeft the zero isn’t added.
There is also an overload of the PadLeft method, this method will add spaces till the total width is met.
public string PadLeft(int totalWidth)
As you probably expected there is also a PadRight method, this will do the opposite of the PadLeft method.
You can find more info and examples on the MSDN pages:
String.PadLeft Method (Int32)
String.PadLeft Method (Int32, Char)
String.PadRight Method (Int32)
String.PadRight Method (Int32, Char)
Nifty .NET Part #2: Enumerable.Empty<T>
In part 2 the generic method Enumerable.Empty, as the name would say it returns a empty IEnumerable of T:
public static IEnumerable Empty()
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
Lets see how it looks in code, for example we create a method that can return a list of strings:
1: using System;
2: using System.Linq;
3: using System.Collections.Generic;
4:
5: public class MyClass
6: {
7: public static void Main()
8: {
9: IEnumerable<string> names = GetNames(true);
10:
11: foreach(string name in names)
12: {
13: Console.WriteLine(name);
14: }
15:
16: Console.ReadLine();
17: }
18:
19: public static IEnumerable<string> GetNames(bool condition)
20: {
21: if(condition)
22: {
23: return new string[] { "Tom", "Robin", "Paul", "Dennis" };
24: }
25:
26: return Enumerable.Empty<string>();
27: }
28: }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
So if condition is true the GetNames method will return an array of names, if not return an empty sequence of strings.
So why not return a string array of size 0 (new string[0]), well if we use Enumerable.Empty is more clearly what we want to do and every empty sequence of T will be cached so there is also some performance improvements.
The MSDN page of Enumerable.Empty:
Empty
Nifty .NET Part #1: String.Join
In the first part of the nifty .NET series I will blog about the method String.Join. First let see how the method definition looks like:
public static string Join(string separator, params string[] value)
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
Like the name suggests this method can join a bunch of strings together with a given separator.
Below an example of the method:
1: using System;
2:
3: public class MyClass
4: {
5: public static void Main()
6: {
7: string[] array = new String[] { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
8:
9: string phrase = String.Join(" ", array);
10:
11: Console.WriteLine(phrase);
12:
13: Console.ReadLine();
14: }
15: }
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
You probably already guessed it, that the output of this console application will be “The quick brown fox jumps over the lazy dog” because I used a space as separator.
With the overload below you can Join a specific part of the array together:
public static string Join(string separator, string[] value, int startIndex, int count)
There are also some more overload and generic variations on the String.Join listed below:
Join(String, IEnumerable)
Join(String, IEnumerable)
Join(String, Object[])
And for the completeness the msdn pages of the methods discussed above:
Join(String, String[])
Join(String, String[], Int32, Int32)
From now on you will find some cases where its very nifty to use the String.Join method.
Nifty .NET Introduction
This is an start of a new blog series called: “Nifty .NET”. In this series I will blog about all the nifty .NET things, like classes, methods, configurations, etc.
Maybe you already know, maybe not, but I want to remind you about the nifty things .NET has, so at the end we all write more beautiful code than before.
I will update this blog post with all the parts of this blog series so we get one useful overview:
Part #1: String.Join
Part #2: Enumerable.Empty
Part #3: String.PadLeft
Part #4: Enumerable.Any