- Home /
setting variables to NULL
hi folks!
how do you set a variable to NULL? i just want to do some kind of reset to it.
thanx!
Answer by Eric5h5 · Apr 28, 2010 at 03:32 PM
myVariable = null;
To clarify: This is assu$$anonymous$$g myVariable
is not a value type (struct), in which case it isn't nullable (unless you define it as such, but that's just a pain to work with and introduces boxing). Vector3 is an example, but for that you could do something like myVariable = Vector3.zero;
man, that was easy - thanx! (i only tried Null and NULL - but hey, its good to find that question and its answer here...)
Is there a fast way to set a big list of variables to null? There is no option to iterate to all the variables?
What do you mean set all the variables to null? Do you mean instantiate a new object or do you mean set it to null in the collection?
Either way:
public class Person
{
public string Name { get; set; } = "Some Default Name";
public int Age { get; set; } = 999;
public override string ToString()
{
return $"{Name} is {Age}";
}
}
var people = new List
{
new Person
{
Name = "Jacob",
Age = 36
},
new Person
{
Name = "Frank",
Age = 12
},
new Person
{
Name = "Alice",
Age = 24
}
};
// Do you mean this?
for (var i = 0; i < people.Count; i++)
{
people[i] = default;
}
// (Outputs: ,,) i.e null x3
Console.WriteLine(String.Join(',', people));
// Or this (Outputs: Some Default Name is 999,Some Default Name is 999,Some Default Name is 999)
for (var i = 0; i < people.Count; i++)
{
people[i] = new Person();
}
Console.WriteLine(String.Join(',', people));