I don't get it. The As
operator:
Then why does the following work?
struct Baby : ILive { public int Foo { get; set; } public int Ggg() { return Foo; } } interface ILive { int Ggg(); } void Main() { ILive i = new Baby(){Foo = 1} as ILive; // ?????? Console.Write(i.Ggg()); // Output: 1 }
Baby
is a struct, creating it will put value in stack
. There is no reference involve here.
There are certainly no nullable types here.
Any explanation as to why I'm wrong?
Structure and Union MembersA structure is a user-defined data type which is a collection of an ordered group of data objects. Unlike the elements of an array, the data objects within a structure are of different data types. Each data object in a structure/union is called a member of the structure/union.
The -> is called the arrow operator. It is formed by using the minus sign followed by a greater than sign. Simply saying: To access members of a structure, use the dot operator. To access members of a structure through a pointer, use the arrow operator.
The Arrow(->) operator exists to access the members of the structure or the unions using pointers.
Remarks. The member access operators . and -> are used to refer to members of struct , union , and class types. Member access expressions have the value and type of the selected member.
Casting it as an interface will create a boxed copy on the managed heap , and return a reference to the boxed copy. The box implements the interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With