Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Testing program for demonstrating C# default keyword.
using System;
using System.Collections.Generic;
using System.Text;
namespace DefaultKeywordTest
{
/// <summary>
/// 顯示 Null 值的 formatter
/// Reference: http://stackoverflow.com/questions/7689040/can-i-format-null-values-in-string-format
/// </summary>
public class CustomizedFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type service)
{
if (service == typeof(ICustomFormatter))
{
return this;
}
else
{
return null;
}
}
public string Format(string format, object arg, IFormatProvider provider)
{
if (arg == null)
{
return "null";
}
IFormattable formattable = arg as IFormattable;
if (formattable != null)
{
return formattable.ToString(format, provider);
}
return arg.ToString();
}
}
/// <summary>
/// 測試用類別
/// </summary>
public class DemoClass
{
/// <summary>
/// Value Type 屬性
/// </summary>
public int ValueTypeProperty { get; set; }
/// <summary>
/// Reference Type 屬性
/// </summary>
public List<int> ReferenceTypeProperty { get; set; }
/// <summary>
/// 顯示字串
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
CustomizedFormatter formatter = new CustomizedFormatter();
sb.AppendFormat(formatter, "ValueTypeProperty => {0}", ValueTypeProperty);
sb.AppendFormat(formatter, ", ReferenceTypeProperty => {0}\r\n", ReferenceTypeProperty);
return sb.ToString();
}
}
/// <summary>
/// 測試用結構
/// </summary>
public struct DemoStruct
{
/// <summary>
/// Value Type 屬性
/// </summary>
public int ValueTypeProperty { get; set; }
/// <summary>
/// Reference Type 屬性
/// </summary>
public List<int> ReferenceTypeProperty { get; set; }
/// <summary>
/// 顯示字串
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
CustomizedFormatter formatter = new CustomizedFormatter();
sb.AppendFormat(formatter, "ValueTypeProperty => {0}", ValueTypeProperty);
sb.AppendFormat(formatter, ", ReferenceTypeProperty => {0}", ReferenceTypeProperty);
return sb.ToString();
}
}
/// <summary>
/// 測試用列舉 (指定每個列舉值,背後代表一個整數)
/// </summary>
public enum DemoEnum : int
{
Value1 = 1,
Value2 = 2
}
/// <summary>
/// 測試用列舉 (不指定每個列舉值,背後代表一個整數)
/// </summary>
public enum DemoEnumWithoutValue : int
{
Value1,
Value2
}
/// <summary>
/// 測試程式進入點
/// </summary>
public class Program
{
public static void RunSnippet()
{
String tmp;
tmp = string.Format(new CustomizedFormatter(), "default(DemoClass) = {0}", default(DemoClass));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(DemoStruct) = {0}", default(DemoStruct));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(DemoEnum) = {0}", default(DemoEnum));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(DemoEnumWithoutValue) = {0}", default(DemoEnumWithoutValue));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(DateTime) = {0}", default(DateTime));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(DateTime?) = {0}", default(DateTime?));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(List<int>) = {0}", default(List<int>));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(string) = {0}", default(string));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(int) = {0}", default(int));
Console.WriteLine(tmp);
tmp = string.Format(new CustomizedFormatter(), "default(int?) = {0}", default(int?));
Console.WriteLine(tmp);
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment