[英]C# setting object as null
I want to set an object as null so I can 'consume' it of sorts. 我想将一个对象设置为null,以便可以“消费”各种对象。 In Java we have this.
在Java中,我们有这个。
//In some function/object
Vector3 position = new Vector3();
//In some loop.
if(position != null){
consumePositionAsForce(position);
position = null;
}
I'm aware in C# that one has to 'Box' the and 'Unbox' the object if you are using primitive types buut I cannot find any documentation on nullable value types. 我知道在C#中,如果您使用的是基本类型buut,则必须将对象“装箱”并“取消装箱”,我找不到关于可空值类型的任何文档。
I'm trying to do the same in C# but I'm getting an errors/warning about type cast. 我正在尝试在C#中执行相同的操作,但是却收到有关类型转换的错误/警告。 As in I cannot set Vector3 = null.
如我不能设置Vector3 = null。
You can use nullable types to do this: 您可以使用可为空的类型来执行此操作:
Vector3? vector = null;
And assign its value from some place: 并从某个位置分配其值:
position = new Vector3();
And then you can easily compare it to null
as you would have compared a reference type object: 然后,您可以像比较引用类型对象一样轻松地将其比较为
null
:
if(position != null) //or position.HasValue if you want
{
//...
}
After you verify it is not null
, to access the Vector3
value, you should use position.Value
. 确认它不为
null
,要访问Vector3
值,应使用position.Value
。
Can you declare it as a nullable Vector3 (Vector3?)? 您可以将其声明为可为空的Vector3(Vector3吗?)?
Vector3? position = null;
That'd be my first suggestion. 那是我的第一个建议。 Alternatively, you could set it to Vector3.Zero, but I don't really like that idea.
另外,您可以将其设置为Vector3.Zero,但我真的不喜欢这个想法。
I'm fairly certain Vector3 is a value type, not a reference type, so you can't assign null to it without explicitly declaring it as a nullable Vector3. 我相当确定Vector3是一个值类型,而不是引用类型,因此您不能在未显式声明为可为空的Vector3的情况下为其分配null。
You can have nullable value types using a the Nullable<T>
where T
is a struct
(primite types) or adding a ?
您可以使用
Nullable<T>
来具有可为空的值类型,其中T
是一个struct
(基本类型)或添加一个?
after as a prefix of the type. 之后作为类型的前缀。 With this you can set, for sample, a int, a Vector , or Vector3d structure as nullable, for sample:
这样,您可以将样本的int, Vector或Vector3d结构设置为可空的样本:
Vector? vector2d = null;
Vector3d? vector3d = null;
When you have nullable types, you has two new properties, which is HasValue
which returns a bool
value indicating if there is a valid value for the object and Value
which return the real value (for a int?
return a int
). 当您具有可为空的类型时,将具有两个新属性,即
HasValue
返回一个bool
值,该bool
值指示该对象是否存在有效值,而Value
返回实际值(对于int?
返回一个int
)。 You could use something like this: 您可以使用如下形式:
// get a structure from a method which can return null
Vector3d? vector3d = GetVector();
// check if it has a value
if (vector3d.HasValue)
{
// use Vector3d
int x = vector3d.Value.X;
}
Actually, the Nullable<T>
class tries to encapsulate the value types as a reference types to give the impression you can set null for a value type. 实际上,
Nullable<T>
类尝试将值类型封装为引用类型,以给您留下可以为值类型设置null的印象。
I think you know but I recommend you read more about boxing and unboxing . 我想您知道,但我建议您阅读更多有关装箱和拆箱的信息 。
Use Vector3?
使用
Vector3?
(nullable Vector3) instead of Vector3
. (可为空的Vector3)而不是
Vector3
。
You can't set a value type to null. 您不能将值类型设置为null。
Since Vector3 is a struct (which is a value type) you won't be able to set it to null as is. 由于Vector3是一种结构(这是一种值类型),因此您将无法按原样将其设置为null。
You could use a nullable type like: 您可以使用可为空的类型,例如:
Vector3? position = null;
but that will require casting it to Vector3 when you want to use it in a function that is looking for regular Vector3. 但是当您要在要查找常规Vector3的函数中使用它时,需要将其强制转换为Vector3。
Vector3 is a struct and therefore is not nullable or disposable. Vector3是一个结构,因此不能为null或可抛弃。 You can either use
您可以使用
Vector3? position = null;
Or you can change it like this: 或者,您可以像这样更改它:
class Program
{
static void Main(string[] args)
{
using (Vector3 position = new Vector3())
{
//In some loop
consumePositionAsForce(position);
}
}
}
struct Vector3 : IDisposable
{
//Whatever you want to do here
}
The struct is now disposable so that you can use it in a using statement. 该结构现在是一次性的,因此您可以在using语句中使用它。 This will kill the object after use.
使用后将杀死该物体。 This is better than nulls since you don't over complicate things and you don't have to worry about missing a null check or event an undisposed object in memory.
这比空值更好,因为您不会使事情复杂化,也不必担心丢失空值检查或发生内存中未处理的对象的事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.