about the interface of C#
-
I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!
-
I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!
Well, a program without an interface can't really do much, can it ? Or do you mean interfaces as a coding construct, not a user interface ? I would suggest you buy a book on C# and read it, rather than hope to get a deep understanding from random forum replies. An interface is useful mostly as a means to get around the lack of multiple inheritance.
Christian Graus Driven to the arms of OSX by Vista. Please read this[^] if you don't like the answer I gave to your question. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
-
I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!
I'm assuming like Christian that your question is about using interfaces in your own classes. Interfaces do not offer any significant performance benefit. However, they are very useful in situations where classes do inherit from the same base type. Here's a quick example of using interfaces: public class SecretService : ISecurityCleared .... public class CIA : ISecurityCleared ... public class President : ISecurityCleared ... //ISecurityCleared interface... public interface ISecurityCleared { void ViewTopSecretMessage(string message); } //sample code... private void TopSecretMessage(string topsecretmessage) { List<ISecurityCleared> securityCleared = new List<ISecurityCleared>(); securityCleared.Add(new SecretService()); securityCleared.Add(new CIA()); securityCleared.Add(new President()); for (int i = 0; i < securityCleared.Count; i++) securityCleared.ViewTopSecretMessage(topsecretmessage); } Again, no performance increase, just cleaner code without having to worry about inheritance issues. Cheers, Richard
There cannot be a crisis today; my schedule is already full.
-
I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!
Another advantage of interfaces (again assuming you are talking about code constructs and not the UI) is that they provide a well defined contract to program against. In other words, if you know that a certain type supports a certain interface, then you know that it implements the methods on that interface. An example:
public interface IMyInterface
{
void DoSomething();
}public class MyInterfaceClass : IMyInterface
{
public void DoSomething()
{
Console.WriteLine("IMyInterface called");
}
}Then, you could do something like the following:
public class MyGenericClass<T> where T : IMyInterface, new()
{
public void CallAMethod()
{
T value = new T();
value.DoSomething();
}
}The generic class is constrained to using classes that support the IMyInterface interface, which means you could write code like this:
new MyGenericClass<MyInterfaceClass>().CallAMethod();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I'm assuming like Christian that your question is about using interfaces in your own classes. Interfaces do not offer any significant performance benefit. However, they are very useful in situations where classes do inherit from the same base type. Here's a quick example of using interfaces: public class SecretService : ISecurityCleared .... public class CIA : ISecurityCleared ... public class President : ISecurityCleared ... //ISecurityCleared interface... public interface ISecurityCleared { void ViewTopSecretMessage(string message); } //sample code... private void TopSecretMessage(string topsecretmessage) { List<ISecurityCleared> securityCleared = new List<ISecurityCleared>(); securityCleared.Add(new SecretService()); securityCleared.Add(new CIA()); securityCleared.Add(new President()); for (int i = 0; i < securityCleared.Count; i++) securityCleared.ViewTopSecretMessage(topsecretmessage); } Again, no performance increase, just cleaner code without having to worry about inheritance issues. Cheers, Richard
There cannot be a crisis today; my schedule is already full.
-
Richard Blythe wrote:
securityCleared.Add(new President());
I wish...
:-D No comment
There cannot be a crisis today; my schedule is already full.
-
I am a novice of C#,Could you please tell me the advantage and disadvantage of using interface and not using interface in the proggram? I'm gragteful if you give a litte code for contrast. Thanks!
Another benefit of interfaces is that they help with code reuse. If a method parameter is an interface type, you can call it by passing an object of ANY class that implements that interface.
-
Another benefit of interfaces is that they help with code reuse. If a method parameter is an interface type, you can call it by passing an object of ANY class that implements that interface.
thanks all for your help!