C# 带有switch的类声明发出警告,提示if语句中的代码是int,但代码的false大小写也可以使用
当使用自定义类时,有一个接受int的开关。该开关然后填充类,其中有些情况下有num,有些情况下没有。int几乎可以在应用程序中的任何位置变化,但有些情况下没有int。查看新类时,用户需要查看哪些类没有编号。以下代码工作正常,但需要删除以下警告: “将值类型C# 带有switch的类声明发出警告,提示if语句中的代码是int,但代码的false大小写也可以使用,c#,class,int,non-nullable,C#,Class,Int,Non Nullable,当使用自定义类时,有一个接受int的开关。该开关然后填充类,其中有些情况下有num,有些情况下没有。int几乎可以在应用程序中的任何位置变化,但有些情况下没有int。查看新类时,用户需要查看哪些类没有编号。以下代码工作正常,但需要删除以下警告: “将值类型newClass.num与null进行比较的结果为true” 和 语句的错误部分检测到“无法访问的代码” 是否有一种方法或最佳实践可用于测试类的空引用部分? 这种情况叫什么(即,不可为空引用,为空引用测试)(…因为我不知道搜索什么) 使用sys
newClass.num
与null进行比较的结果为true
”
和
语句的错误部分检测到“无法访问的代码”
是否有一种方法或最佳实践可用于测试类的空引用部分?
这种情况叫什么(即,不可为空引用,为空引用测试)(…因为我不知道搜索什么)
使用system.generic;
使用system.generic.collections;
公共类新类{
字符串名;
int-num;
公共类(整数索引){
开关(索引){
案例1:
num=20;
name=“返回true”;
打破
案例2:
//这里没有数字
name=“返回false”;
违约:
打破
}
}
}
公共其他类{
newClass foo=新的newClass(1);
新类别栏=新类别(2);
List newClassList=newlist();
newClassList.add(foo);
newClassList.add(bar);
foreach(新类列表中的新类nc){
如果(nc.num!=null){
打印(“真实”);
}否则{
印刷品(“假”);
}
}
}
该值定义为int
,它是C#中的一种值类型,因此不可为空。您可以通过将警告设置为int?
,即可为空的整数来删除警告。或者,如果在您的场景中,0
不是num
的可能值,请将您的条件更改为:
// default(int) will be 0, the default value for a C# int
if(nc.num != default(int)){
print("True");
} else {
print("False");
}
该值定义为
int
,它是C#中的值类型,因此不可为null。您可以通过将警告设置为int?
,即可为空的整数来删除警告。或者,如果在您的场景中,0
不是num
的可能值,请将您的条件更改为:
// default(int) will be 0, the default value for a C# int
if(nc.num != default(int)){
print("True");
} else {
print("False");
}
不能使用null检查变量,除非它是可为null的(如字符串) 所以:1)修改:int num; 成为: 智力?num 2) 除去其他
foreach (newClass nc in newClassList)
{
if (nc.num != null) //warning will disappear because num is nullable
{
print("True");
}
print("False");
}
我修改了您的代码以避免编译错误
using System.Collections.Generic;
public class newClass
{
public string name;
public int? num;
public newClass(int index)
{
switch (index)
{
case 1:
num = 20;
name = "returns true";
break;
case 2:
// no num here
name = "returns false";
break;
default:
break;
}
}
}
public class otherClass
{
newClass foo = new newClass(1);
newClass bar = new newClass(2);
List<newClass> newClassList = new List<newClass>();
public otherClass()
{
newClassList.Add(foo);
newClassList.Add(bar);
foreach (newClass nc in newClassList)
{
if (nc.num != null)
{
print("True");
}
print("False");
}
}
private void print(string msg)
{
throw new System.NotImplementedException();
}
}
使用System.Collections.Generic;
公共类新类
{
公共字符串名称;
公共整数;
公共类(整数索引)
{
开关(索引)
{
案例1:
num=20;
name=“返回true”;
打破
案例2:
//这里没有数字
name=“返回false”;
打破
违约:
打破
}
}
}
公共类其他类
{
newClass foo=新的newClass(1);
新类别栏=新类别(2);
List newClassList=newlist();
公共类()
{
newClassList.Add(foo);
newClassList.Add(bar);
foreach(新类列表中的新类nc)
{
如果(nc.num!=null)
{
打印(“真实”);
}
印刷品(“假”);
}
}
私有无效打印(字符串消息)
{
抛出新系统。NotImplementedException();
}
}
您不能用null检查变量,除非它可以为null(如字符串)
所以:1)修改:int num;
成为:
智力?num
2) 除去其他
foreach (newClass nc in newClassList)
{
if (nc.num != null) //warning will disappear because num is nullable
{
print("True");
}
print("False");
}
我修改了您的代码以避免编译错误
using System.Collections.Generic;
public class newClass
{
public string name;
public int? num;
public newClass(int index)
{
switch (index)
{
case 1:
num = 20;
name = "returns true";
break;
case 2:
// no num here
name = "returns false";
break;
default:
break;
}
}
}
public class otherClass
{
newClass foo = new newClass(1);
newClass bar = new newClass(2);
List<newClass> newClassList = new List<newClass>();
public otherClass()
{
newClassList.Add(foo);
newClassList.Add(bar);
foreach (newClass nc in newClassList)
{
if (nc.num != null)
{
print("True");
}
print("False");
}
}
private void print(string msg)
{
throw new System.NotImplementedException();
}
}
使用System.Collections.Generic;
公共类新类
{
公共字符串名称;
公共整数;
公共类(整数索引)
{
开关(索引)
{
案例1:
num=20;
name=“返回true”;
打破
案例2:
//这里没有数字
name=“返回false”;
打破
违约:
打破
}
}
}
公共类其他类
{
newClass foo=新的newClass(1);
新类别栏=新类别(2);
List newClassList=newlist();
公共类()
{
newClassList.Add(foo);
newClassList.Add(bar);
foreach(新类列表中的新类nc)
{
如果(nc.num!=null)
{
打印(“真实”);
}
印刷品(“假”);
}
}
私有无效打印(字符串消息)
{
抛出新系统。NotImplementedException();
}
}
num
是一种int
,它是一种值类型,因此不能为null
。num
不可为null。num
是一种int
值类型,因此它不能为null
。num不可为null。谢谢。智力?绝对是值得一看的。这一个问号肯定是解决办法。谢谢。智力?绝对是值得一看的。这一个问号肯定是解决办法。