N /整数类型问题 [英] N/a integer type problem
问题描述
Plase,我有一个带有整数属性的类,但是其中一些是N / A - 不可用,我怎么能用整数写,但不能用零?
例如
public int speed;
然后我上课了车和速度,我简化了:
car c1 =新车(250);
车c2 =新车(300);
车c =新车(N / A); //这辆车是原型,我们还不知道速度,我怎么写... N / A?不改变valute类型整数?因为所有其他人都使用整数,我希望它是int nubmers ...
我尝试了什么:
尝试使用零值,但这不是我想要的......:/
Plase, I have a class with integers attributes, but some of them are N/A - not avilable, how can I write in using integer, but not using zero?
e.g.
public int speed;
and then I have class with cars and speeds, I simplified:
car c1 = new car(250);
car c2 = new car(300);
car c = new car(N/A); // and this car is a prototype and we don´t know the speed yet, how do I write .. N/A? without changing valute type integer? cause all of the others are using integer and I want it to be int nubmers...
What I have tried:
tried using zero value, but it is not exactly what I want ... :/
推荐答案
尝试使用null-able类型和传递空值:
Try using null-able types and passing nulls:
public class car
{
public car(int? init)
{
value = init;
}
private int? value;
...
}
var c = new car(null); // n/a
使用可空的int
Use a nullable int
int? x;
x = null;
if (x.HasValue)
{
Debug.WriteLine(x.Value);
}
x = 123;
if (x.HasValue)
{
Debug.WriteLine(x.Value);
}
那么让你的Car构造函数接受int吗?作为一个参数,您可以为无值传递null。
So make your Car constructor accept int? as a param and you can pass null for "no value".
这篇关于N /整数类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!