Sample program using interface in c#




















While static fields are now permitted, instance fields are not permitted in interfaces. Instance auto-properties are not supported in interfaces, as they would implicitly declare a hidden field.

This rule has a subtle effect on property declarations. In an interface declaration, the following code does not declare an auto-implemented property as it does in a class or struct. Instead, it declares a property that doesn't have a default implementation but must be implemented in any type that implements the interface:. An interface can inherit from one or more base interfaces. When an interface overrides a method implemented in a base interface, it must use the explicit interface implementation syntax.

When a base type list contains a base class and interfaces, the base class must come first in the list. A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface. WriteLine "January" ;. WriteLine "February" ;. WriteLine "March" ;. WriteLine "April" ;. WriteLine "May" ;. WriteLine "June" ;. WriteLine "July" ;. WriteLine "August" ;.

WriteLine "September" ;. WriteLine "October" ;. WriteLine "November" ;. WriteLine "December" ;. WriteLine "you did not enter correct value for month name" ;. Write "Enter total days:" ;. WriteLine "Canceled your Membership" ;. Write "Enter the Year :" ;. Next 1, ; Console. Next 1 , ;. WriteLine numbers [ i ] ;. Write "Base Number : " ;. Write "exponent Number : " ;. Abs expNumber ;. ReadLine ; while expNumber! WriteLine "Enter the Number" ;.

Write "Number :" ;. WriteLine ;. Write "Enter positive number1 :" ;. Write "Enter positive number2 :" ;. Write "Enter something for to check that is it palindrome :" ;. ToString ;. ToString number, 2 ; Console. Write "Enter a number : " ;. ToString number , 2 ;.

Write "Input the size of array : " ;. ReadKey true ; if chr. TryParse chr. KeyChar; Console. Write chr. Substring 0, num. In C , we cannot create objects of an abstract class. We use the abstract keyword to create an abstract class. For example,. An abstract class can have both abstract methods method without body and non-abstract methods method with the body. Before moving forward, make sure to know about C inheritance.

As we cannot create objects of an abstract class, we must create a derived class from it. So that we can access members of the abstract class using the object of the derived class. In the above example, we have created an abstract class named Language. The class contains a non-abstract method display. We have created the Program class that inherits the abstract class. Notice the statement,.

Here, obj is the object of the derived class Program. We are calling the method of the abstract class using the object obj.

Note : We can use abstract class only as a base class. This is why abstract classes cannot be sealed. To know more, visit C sealed class and method. A method that does not have a body is known as an abstract method. We use the abstract keyword to create abstract methods. Here, display is an abstract method. Same as in C Inheritance , we use : symbol to implement an interface. For example,. In the above example, we have created an interface named IPolygon. The interface contains a method calculateArea int a, int b without implementation.

Here, the Rectangle class implements IPolygon. And, provides the implementation of the calculateArea int a, int b method. Note : We must provide the implementation of all the methods of interface inside the class that implements it. We have implemented both interfaces in the Rectangle class separated by ,. The interface contains a method calculateArea int l, int b without implementation.

Notice, we have used the reference variable of interface IPolygon. It points to the class Rectangle that implements it. Though we cannot create objects of an interface, we can still use the reference variable of the interface that points to its implemented class. In the above program, we have created an interface named IPolygon.

It has an abstract method calculateArea.



0コメント

  • 1000 / 1000