Quick actions

cmd+k|ctrl+k

Navigation

Languages

C# existential type

Snippet info

Language

Csharp

Visibility

public

Author

740243525

Created

2020-01-30T15:43:43Z

Updated

2020-01-30T15:43:43Z

using System;

class MagicBox{
    private Object member;
    private Type type;
    public static MagicBox createMagicBox<T>(T obj){
        MagicBox box=new MagicBox();
        box.member=obj;
        box.type=typeof(T);
        return box;
    }
    public T unwrap<T>(){
        return (T)member;
    }
    public bool is_a<T>(){
        Type t=typeof(T);
        return type==t;
    }
    
}

class MainClass {
    static void Main() {
        Console.WriteLine("Hello World!");
        MagicBox box=MagicBox.createMagicBox(20);
        if(box.is_a<int>()){
            Console.WriteLine("Yes. "+box.unwrap<int>());
        }else{
            Console.WriteLine("Nope.");
        }
        if(box.is_a<float>()){
            Console.WriteLine("Yes.");
        }else{
            Console.WriteLine("Nope.");
        }
    }
}
INFO