Files
2025-10-13 09:28:20 +08:00

34 lines
1.1 KiB
C#

namespace Lesson5_泛型练习
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine(InputType<int>());
Console.WriteLine(InputType<float>());
Console.WriteLine(InputType<double>());
Console.WriteLine(InputType<string>());
Console.WriteLine(InputType<decimal>());
}
public static string InputType<T>()
{
if (typeof(T) == typeof(int))
{
return string.Format("{0},{1}字节", "整形", sizeof(int));
}else if (typeof(T) == typeof(float))
{
return string.Format("{0},{1}字节", "单精度浮点数", sizeof(float));
}else if (typeof(T) == typeof(double))
{
return string.Format("{0},{1}字节", "双精度浮点数", sizeof(double));
}else if (typeof(T) == typeof(string))
{
return string.Format("{0},{1}字节", "字符串", "不定长");
}
return "其他类型";
}
}
}