ぽにょろん

思いついたこととメモ

c# で default(T) した時の値

default(string) が Null なのをよく忘れるので、 default(T) の値をまとめてみました。

class Program
{
    static void Main(string[] args)
    {
        $@"dyte: {default(byte)}".Dump();  //0
        $@"sbyte: {default(sbyte)}".Dump();  //0 
        $@"int: {default(int)}".Dump();  //0 
        $@"uint: {default(uint)}".Dump();  //0 
        $@"short: {default(short)}".Dump();  //0 
        $@"ushort: {default(ushort)}".Dump();  //0 
        $@"long: {default(long)}".Dump();  //0 
        $@"ulong: {default(ulong)}".Dump();  //0 
        $@"float: {default(float)}".Dump();  //0 
        $@"double: {default(double)}".Dump();  //0 
        $@"char(hex): {Convert.ToInt32(default(char)):X}".Dump();  //0 
        $@"bool: {default(bool)}".Dump();  //false
        $@"object: {default(object) ?? "null"}".Dump();  //null
        $@"string: {default(string) ?? "null"}".Dump();  //null
        $@"decimal: {default(decimal)}".Dump();  //0
        var sample = default(Sample1);
        $@"struct(Prop1): {sample.Prop1 ?? "null"}".Dump();  //null
        $@"struct(Prop2): {sample.Prop2}".Dump();  //0
        $@"Class(isNull): {default(Sample2) == null}".Dump();  //null
        $@"Enum: {default(EnumSample)}".Dump();  //num1
        Console.ReadLine();
    }
}
public enum EnumSample { num1, num2, num3 }
public struct Sample1 { public string Prop1; public int Prop2; }
public class Sample2
{
    public string Prop1 { get; set; }
    private int Prop2 { get; set; }
}
public static class StringExtension
{
    public static void Dump(this string input)
    {
        Console.WriteLine(input);
    }
}

f:id:kowill:20150906110159p:plain