Skip to main content
 首页 » 编程设计

c#之在 C# 中将 null 传递给 Web 方法

2024年01月01日51telwanggs

我已经给出了一个网络服务 Url。它的一种方法有一个论点,如果我传递 null 它会做一些事情,如果传递 int 会做其他事情。我在项目中添加了服务引用。 C# 不接受将 null 传递给该参数,因为它已实现其接口(interface)以接受 int 而不是 int? (可为空的整数)。

请指教。

请您参考如下方法:

您还可以做一个可怕的漫游,解析 int.MinValue 或 int.MaxValue。我真的不喜欢这种解决方案,这不是最佳做法,但可以解决问题。

例如

public void DoSomething( int numofSomething ) 
{ 
    if ( numofSomething == int.MinValue ) 
    { 
        // act as if numofSomething is null 
    } 
    else 
    { 
        // act as normal 
    } 
} 

请记住,您正在编写更糟糕的代码,因为您使用最小值或最大值来表示空值。仅当无法更改界面时才执行此操作。