添加Lesson5

This commit is contained in:
2025-10-13 09:28:20 +08:00
parent 6cc1b0d4f9
commit 24da5481a1
5 changed files with 201 additions and 0 deletions
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+33
View File
@@ -0,0 +1,33 @@
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 "其他类型";
}
}
}