+ 1

Is a Interface a reference type ?

I am exploring all types in the .net framework. This one confuses me. It is referred to as reference type but does not act like one.

1st Dec 2019, 6:41 PM
sneeze
sneeze - avatar
13 RĂ©ponses
+ 2
Oh...and after this we can use .GetType() for this variable, and from this type we can get BaseType! https://code.sololearn.com/ckK8sPnc5oti/?ref=app
2nd Dec 2019, 2:57 PM
id001x
id001x - avatar
+ 2
Hi! Can you explain more detailed , what are you mean when say "...but does not act like one" ?
1st Dec 2019, 7:34 PM
id001x
id001x - avatar
+ 2
I beg your pardon for my english and mistakes, if thay would be there, and i'm sorry for my question inside your question, but it related questions.. I think about this, but it don't give good result, and i can't answer to your question. But i got a some another question... What interface doing in runtime? All time before your question i thinked that "Interface" - working only inside a compiller, some like "note" that need for remember to impliment some method. But when you said that it "Reference type" - it must be mean that interface in runtime stored in heap.... But why?? ... Why we need interface in heap?? Whithout class or struct that's implement it he can not do anything... Interface - is not independent element in C#, like and delegate, but delegate contain reference on method, this reference can be dynamically changed in runtime, but interface can't contain anything what we need in runtime... It really need only for remember about methods, indexers, events and properties. What do you think about this?
2nd Dec 2019, 2:04 PM
id001x
id001x - avatar
+ 2
Ok, i'm find mistake. We can create variable interface type which contain reference to object of a class which implement this interface. Some like this. It answer for my question... i try thinking next... https://code.sololearn.com/ckK8sPnc5oti/?ref=app
2nd Dec 2019, 2:48 PM
id001x
id001x - avatar
+ 2
You're welcome! It really interesting question!! But if you want i try confuse you again, i want write it tomorrow, but i can't wait 😀 not only class can implement an inteface, also we have a struct. Try it use and see the magic!
2nd Dec 2019, 9:43 PM
id001x
id001x - avatar
+ 2
Yes, now i understand that mean a "contract" in interface description.
4th Dec 2019, 11:53 AM
id001x
id001x - avatar
+ 1
You can not create a instance of a interface And a interface does not seem to inherited from system.object. https://code.sololearn.com/cP28QGks7XOf I can get the basetype of a class, and the base type of a a delegate. But this does not seem to work this way for a interface.
1st Dec 2019, 7:42 PM
sneeze
sneeze - avatar
+ 1
Thanks you did it. https://code.sololearn.com/crBuinRNqt14 This code proofs even the interface-type inherited from system.object Because the hierachy does not show System.ValueType, it is a reference type. Or another explanation I read. A valuetype has a fixed size. A reference type has a dynamic size. Since a interface type is typically something with a dynamic size it is a reference type.
2nd Dec 2019, 9:35 PM
sneeze
sneeze - avatar
+ 1
Special appreciation for writing english. I can understand it and I really appreciate the effort you did to answer me.
2nd Dec 2019, 9:37 PM
sneeze
sneeze - avatar
+ 1
Please do. I can't wait. I know structs can implement interfaces. It is fun to proof the theory behind all this.
2nd Dec 2019, 9:46 PM
sneeze
sneeze - avatar
+ 1
Yes is confuses me. To be continued. The bottom line is a interface is not really a type (it is a contract) and should not be categorize like one.
3rd Dec 2019, 9:58 PM
sneeze
sneeze - avatar
+ 1
Adding interface to explanation https://code.sololearn.com/coNxJU36PwdF/?ref=app
5th Dec 2019, 8:46 AM
id001x
id001x - avatar