What exactly (under the hood) do the +=
and -=
operators do?
Or are they implicit in that they are defined per type?
I've used them extensively, it's a very simple feature of the syntax, but I've never thought about the depths of how it works.
What Brought About the Question
I can concatenate a string value like so:
var myString = "hello ";
myString += "world";
All fine. But why doesn't this work with collections?
var myCol = new List<string>();
myCol += "hi";
You may say 'well you're attempting to append a different type, you can't append a string to a type that isn't string'. But the following doesn't work either:
var myCol = new List<string>();
myCol += new List<string>() { "hi" };
Ok, maybe it doesn't work with collections, but is the following not a (kind of) collection of event handlers?
myButton.Click += myButton_Click;
I'm obviously lacking an in-depth understanding of how these operators work.
Please note: I'm not trying to build the collection myCol
in this way, in a real project. I'm merely curious about the workings of this operator, it's hypothetical.
question from:
https://stackoverflow.com/questions/40503509/how-exactly-can-and-operators-be-interpreted 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…