void Main()
{
Foo();
Foo(null)
Foo(null as object)
}
void Foo(params object[] args)
{
if (args == null)
Console.WriteLine("Null");
else
Console.WriteLine(args.Length);
}
* This source code was highlighted with Source Code Highlighter.
Tiny notes from everyday work. Mostly about .NET, C# and some tools I use every day.
Sunday, August 22, 2010
#1 Params
A simple question. What would be printed out after execution?
Thursday, August 19, 2010
#3 Enum.ToString()
It's kinda surprise, but Enum.ToString() uses reflection to get a string representation of an enum:
This issue could be fixed with the following trick code:
public override string ToString()
{
Type type = base.GetType();
object obj2 = ((RtFieldInfo) GetValueField(type)).InternalGetValue(this, false);
return InternalFormat(type, obj2);
}
* This source code was highlighted with Source Code Highlighter.
This approach leads to visible performance issues when an application widely uses enum.ToString() (Obviously, any enum).This issue could be fixed with the following trick code:
static class EnumHelper
{
static class Cache<T>
{
internal static readonly Dictionary<T, string> Values = new Dictionary<T, string>();
}
public static string FastToString<T>(this T @enum) where T:struct
{
if (!typeof(T).IsEnum)
throw new ArgumentException(string.Format("Type {0} is not an enumeration.", typeof(T)));
lock (Cache<T>.Values)
{
string result;
if (!Cache<T>.Values.TryGetValue(@enum, out result))
{
result = @enum.ToString();
Cache<T>.Values.Add(@enum, result);
}
return result;
}
}
}
* This source code was highlighted with Source Code Highlighter.
And, of course, all enum.ToString() should be replaced with enum.FastToString().
Wednesday, August 18, 2010
Total Commander Find Files and Subversion
When you're trying to search some file in a folder under subversion control, you receive a slight performance degradation, especially when you trying to find some text inside files. Total Commander tries to search inside ".svn" files and folders.
There is a good trick for such cases - Total Commander supports some tiny syntax to be used in "Search for:" field. So to exclude some files or folders from the search, you can put them into "Search field:" after a '|' sign. Note that wildcards can be used in include/exclude directory names, and the names must have a trailing backslash '\'.
For example:
There is a good trick for such cases - Total Commander supports some tiny syntax to be used in "Search for:" field. So to exclude some files or folders from the search, you can put them into "Search field:" after a '|' sign. Note that wildcards can be used in include/exclude directory names, and the names must have a trailing backslash '\'.
For example:
- w*.*|*.bak *.old finds files, which start with w and do not end with .bak or .old.
- SomeFile.cs | .svn\ finds 'SomeFile.cs' not looking into subversion folders.
#2.5 Usings and Enumerators
One more thing regrading using. Do not forget, that IEnumerator<T> is IDisposable (though, IEnumerator is not).
So do not forget to enclose GetEnumerator() calls to using statement.
public interface IEnumerator<T> : IDisposable, IEnumerator
{
// Properties
T Current { get; }
}
* This source code was highlighted with Source Code Highlighter.
So do not forget to enclose GetEnumerator() calls to using statement.
#2 Usings
Just not to forget, that there could be multiple objects in a using statement:
Though, all objects should be of the same type, the following is not allowed:
using (Font font3 = new Font("Arial", 10.0f),
font4 = new Font("Arial", 10.0f))
{
// Use font3 and font4.
}
* This source code was highlighted with Source Code Highlighter.
Though, all objects should be of the same type, the following is not allowed:
using (IEnumerator<T1> firstEnumerator = first.GetEnumerator(),
IEnumerator<T2> secondEnumerator = second.GetEnumerator())
{
// ...
}
* This source code was highlighted with Source Code Highlighter.
Subscribe to:
Posts (Atom)