using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a key,please!");
bool isTrue = true;
while (isTrue)
{
ConsoleKeyInfo readkey = Console.ReadKey();
Console.WriteLine();
string inputkey = GetString(readkey);//这样返回null,是没有问题的
if (null == inputkey)
Console.WriteLine("You entered 'A'");
else if (ConsoleKey.Q == readkey.Key)
isTrue = false;
else
Console.WriteLine(inputkey);
}
Console.ReadKey();
}
static string GetString(ConsoleKeyInfo key)
{
if (ConsoleKey.A == key.Key)
return null;
else
return "hello world";
}
}
}
注:
1.值类型的返回值不能是null
2.返回值为null并赋值没问题