返回值为布尔值的方法

本文关键字:方法 布尔值 返回值 | 更新日期: 2023-09-27 17:59:13

我有下面的代码,其中ShellExecuteEx在执行时返回布尔值true或false。我把它转换成字符串,分配给一个类级别的变量。

strShellCallStatus = ShellExecuteEx(ref info).ToString();
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
public static void exev()
{
    SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
    info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
    info.lpVerb = "open";
    info.lpFile = "c:''windows''notepad.exe";
    info.nShow = 5;
    info.fMask = 0x440;
    info.hwnd = IntPtr.Zero;
    strShellCallStatus = ShellExecuteEx(ref info).ToString();
}

我应该担心ShellExecuteEx返回null值吗?如果是这样,我想使用以下语句:

strShellCallStatus = Convert.ToString(ShellExecuteEx(ref info));

返回值为布尔值的方法

只要ShellExecuteEx签名不是bool? ShellExecuteEx(),就不必担心它会返回null,因为bool是具有默认false的值类型。

简单地说,签名为bool ShellExecuteEx()的方法不能返回null,因为它甚至不会编译。