Basic concepts of interfaces in C #

Source: Internet
Author: User

1. References to InterfacesWe cannot access interfaces directly through the members of a class object, and can be cast to an interface type through a class object reference to obtain a reference to an interface. with the reference to the interface, you can use the dot number to invoke the method of the interface
Understanding: This is a concept of polymorphism, after assigning the object to the interface, through a unified interface name access can achieve a variety of behavior
2. Use the Return interface method (increase the flexibility of the method so that the orientation can have properties and methods like an object)Public IEnumerator GETNUMERAOTR ();This method returns an object that inherits the IEnumerator interface, and can invoke the extension method or property of the interface through the return value of the function.Getnumerator (). The current property can return a value.C # uses several basic concepts to iterate over complex functions iteratively, and then use complex functions to develop more complex functions.
Common Interfaces
  
 
  1. static void Main(string[] args)
  2. {
  3. using (CaryClass caryClass = new CaryClass()) // IDisposeable接口用using来调用
  4. {
  5. caryClass.DoSomething();
  6. }
  7. CaryClass caryClass2 = new CaryClass();
  8. try
  9. {
  10. caryClass2.DoSomething();
  11. }
  12. finally // use finally to make an excuse call
  13. {
  14. IDisposable disposable = caryClass2 as IDisposable;
  15. if (disposable != null)
  16. disposable.Dispose();
  17. }
  18. Console.ReadKey();
  19. }
  20. }
  21. public class CaryClass : IDisposable
  22. {
  23. public void DoSomething()
  24. {
  25. Console.WriteLine("Do some thing....");
  26. }
  27. public void Dispose()
  28. {
  29. Console.WriteLine("及时释放资源");
  30. }
  31. }






From for notes (Wiz)

Basic concepts of interfaces in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.