添加Lesson5
This commit is contained in:
@@ -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>
|
||||
@@ -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 "其他类型";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user