Chain ?? operator
19/01/2010 Leave a comment
You can chain more than one nullable variable and if all null it return string.Empty
string value = valx ?? valy?? valz ?? string.Empty;
Nice isnt it ?
All my posts are provided "AS IS" with no warranties, and confer no rights.
19/01/2010 Leave a comment
You can chain more than one nullable variable and if all null it return string.Empty
string value = valx ?? valy?? valz ?? string.Empty;
Nice isnt it ?
19/01/2010 Leave a comment
when working on client machine you can use server type garbage collector.But it uses more memory.By default in server OS uses Server type garbage collector. You can configure in your web config like
<configuration>
<runtime>
<gcServer enabled="true"/>
</runtime>
</configuration>
The CLR has two different GCs: Workstation (mscorwks.dll) and Server (mscorsvr.dll). When running in Workstation mode, latency is more of a concern than space or efficiency. A server with multiple processors and clients connected over a network can afford some latency, but throughput is now a top priority. Rather than shoehorn both of these scenarios into a single GC scheme, Microsoft has included two garbage collectors that are tailored to each situation.
Server GC:
Workstation GC:
The server GC is designed for maximum throughput, and scales with very high performance. Memory fragmentation on servers is a much more severe problem than on workstations, making garbage collection an attractive proposition. In a uniprocessor scenario, both collectors work the same way: workstation mode, without concurrent collection. On an MP machine, the Workstation GC uses the second processor to run the collection concurrently, minimizing delays while diminishing throughput. The Server GC uses multiple heaps and collection threads to maximize throughput and scale better.
Source: MSDN
18/01/2010 Leave a comment
Aliased generics:
using ASimpleName = Dictionary<string, Dictionary<string, List<string>>>;
It allows you to use ASimpleName, instead of Dictionary<string, Dictionary<string, List<string>>>.
Use it when you would use the same generic big long complex thing in a lot of places.
18/01/2010 Leave a comment
I find very nice Extention for enums and i want to share it.
[Flags]
public enum ErrorTypes : int {
None = 0,
MissingPassword = 1,
MissingUsername = 2,
PasswordIncorrect = 4
}
public static class EnumExtensions {
public T Append<T>(this System.Enum type, T value) {
return (T)(object)(((int)(object)type | (int)(object)value));
}
public static T Remove<T>(this System.Enum type, T value) {
return (T)(object)(((int)(object)type & ~(int)(object)value));
}
public static bool Has<T>(this System.Enum type, T value) {
return (((int)(object)type & (int)(object)value) == (int)(object)value);
}
}
...
//used like the following...
ErrorTypes error = ErrorTypes.None;
error = error.Append(ErrorTypes.MissingUsername);
error = error.Append(ErrorTypes.MissingPassword);
error = error.Remove(ErrorTypes.MissingUsername);
//then you can check using other methods
if (error.Has(ErrorTypes.MissingUsername)) {
...
}
http://stackoverflow.com/questions/9033/hidden-features-of-c/407325#407325
18/01/2010 Leave a comment
Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration.
expression is evaluated and returned as a value to the enumerator object; expressionhas to be implicitly convertible to the yield type of the iterator.
The yield statement can only appear inside an iterator block, which might be used as a body of a method, operator, or accessor. The body of such methods, operators, or accessors is controlled by the following restrictions:
A yield statement cannot appear in an anonymous method. For more information, seeAnonymous Methods (C# Programming Guide).
When used with expression, a yield return statement cannot appear in a catch block or in a try block that has one or more catch clauses. For more information, seeException Handling Statements (C# Reference).
using System;
using System.Collections.Generic;
public class MyClass
{
public static IEnumerable<int> Powers(int from,int to)
{
for (int i = from; i <= to; ++i)
{
yield return (int)Math.Pow(2, i);
}
}
static void Main()
{
IEnumerable<int> powers = Powers(0, 16);
foreach (int result in powers)
{
Console.WriteLine(result);
}
}
}
Source :MSDN