What exactly (under the hood) do the += and -= operators do?
Or are they implicit in the fact that they are defined for each type?
I used them extensively, this is a very simple syntax feature, but I never thought about the depths of how this works.
What caused the question
I can bind the string value as follows:
var myString = "hello "; myString += "world";
All perfectly. But why does this not work with collections?
var myCol = new List<string>(); myCol += "hi";
You can say: "It's good that you are trying to add another type, you cannot add a string to a type that is not a string." But the following does not work:
var myCol = new List<string>(); myCol += new List<string>() { "hi" };
Well, maybe it doesn't work with collections, but is () a collection of event handlers?
myButton.Click += myButton_Click;
I clearly lack a deep understanding of how these operators work.
Please note: I am not going to create myCol collection this way in a real project. I'm just wondering how this operator works, it's hypothetical.
operators c #
JᴀʏMᴇᴇ Nov 09 '16 at 9:24 2016-11-09 09:24
source share