C#:
using System;
using System.Collections.Generic;
public class ICantEvenNameClassAsMainBecauseCSSucks
{
public static void Main(string[] args)
{
body.Add("lol", 4);
Console.WriteLine("C# гавно! {0}", get("lol", 0L));
}
private static Dictionary<string, object> body = new Dictionary<string, object>();
public static T get<T>(string key, T defaultValue)
{
object o;
if (body.TryGetValue(key, out o))
{
return (T) o;
}
else
return defaultValue;
}
}
Unhandled Exception:
System.InvalidCastException: Cannot cast from source type to destination type.
at IEvenCantNameClassAsMainBecauseCSSucks.get[Int64] (System.String key, Int64 defaultValue) [0x00000] in <filename unknown>:0
at IEvenCantNameClassAsMainBecauseCSSucks.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
at IEvenCantNameClassAsMainBecauseCSSucks.get[Int64] (System.String key, Int64 defaultValue) [0x00000] in <filename unknown>:0
at IEvenCantNameClassAsMainBecauseCSSucks.Main (System.String[] args) [0x00000] in <filename unknown>:0
Java:
import java.util.*;
public class Main
{
private static Map<String, Object> body = new HashMap<>();
public static void main(String... args)
{
body.put("lol", 4);
System.out.printf("Java получше! %d\n", get("lol", 0L));
}
public static <T> T get(String key, T defaultValue)
{
Object o;
if (body.containsKey(key))
{
o = body.get("lol");
return (T) o;
}
else
{
return defaultValue;
}
}
}
out:
Java получше! 4
тут даже нечего комментировать