What this mean ??. This code works fine
-
public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List<Item>() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } } :doh:
-
public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List<Item>() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } } :doh:
MSDN wrote:
The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.
:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List<Item>() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } } :doh:
The ?? is known as the null coalescing operator. That's a fancy dance way of saying that it tests to see if the left hand side is null, and if it is, it uses the right hand side. What this is doing, in your example, is evaluate items to see if it's null, and if it is, it assigns a new list to it and returns that. In practical terms, this is the same as doing this:
if (items == null)
items = new List<Item>();
return itemsI have missed the addition of the Item instances to the array to simplify this example, but you should get the idea from it. [Edit]The OP deleted the original message. It was: public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } }
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
The ?? is known as the null coalescing operator. That's a fancy dance way of saying that it tests to see if the left hand side is null, and if it is, it uses the right hand side. What this is doing, in your example, is evaluate items to see if it's null, and if it is, it assigns a new list to it and returns that. In practical terms, this is the same as doing this:
if (items == null)
items = new List<Item>();
return itemsI have missed the addition of the Item instances to the array to simplify this example, but you should get the idea from it. [Edit]The OP deleted the original message. It was: public class Item { public double X { get; set; } public double Y { get; set; } public string Name { get; set; } public string Color { get; set; } } public class ItemsFactory { private List items; public IEnumerable Items { get { return items ?? (items = new List() { new Item { Name = "One", X = 33, Y = 25, Color = "Red" }, new new Item { Name = "Two", X = 44, Y = 99, Color"Blue" } }); } } }
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
You put it much better than MSDN! :)
It’s not because things are difficult that we do not dare, it’s because we do not dare that things are difficult. ~Seneca
Thanks Annie. That's because, unlike MSDN, I'm trying to educate. :-D
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
MSDN wrote:
The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.
:)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Thanks Annie. That's because, unlike MSDN, I'm trying to educate. :-D
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Why did you delete your question? Don't do that.
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
-
Why did you delete your question? Don't do that.
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
I have edited my answer to show what the OP asked.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility