interface

Pattern for exposing non-generic version of generic interface

我们两清 提交于 2019-12-20 08:49:07
问题 Say I have the following interface for exposing a paged list public interface IPagedList<T> { IEnumerable<T> PageResults { get; } int CurrentPageIndex { get; } int TotalRecordCount { get; } int TotalPageCount { get; } int PageSize { get; } } Now I want to create a paging control public class PagedListPager<T> { public PagedListPager<T>(IPagedList<T> list) { _list = list; } public void RenderPager() { for (int i = 1; i < list.TotalPageCount; i++) RenderLink(i); } } The paging control has no

Comparison : interface methods vs virtual methods vs abstract methods

情到浓时终转凉″ 提交于 2019-12-20 08:34:01
问题 What are the advantages and disadvantages of each of these? interface methods virtual methods abstract methods When one should choose what? What are the points one should keep in mind when making this decision? 回答1: Virtual and abstract are almost the same. A virtual method has an implementation in the base class that can optionally be overridden, while an abstract method hasn't and must be overridden in a child class. Otherwise they are the same. Choosing between them depends on the

How is duck typing different from the old 'variant' type and/or interfaces?

烈酒焚心 提交于 2019-12-20 08:09:17
问题 I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide an example of something that i would have to use duck typing to accomplish? I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not

How is duck typing different from the old 'variant' type and/or interfaces?

不打扰是莪最后的温柔 提交于 2019-12-20 08:08:12
问题 I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide an example of something that i would have to use duck typing to accomplish? I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not

Delegates vs Interfaces in C#

喜你入骨 提交于 2019-12-20 08:01:03
问题 I would like to pose this question as long as I am trying currently to dig into the use and the purpose of delegates, although it is likely to have been asked in similar formulations. I know that delegates serve as function pointers used in C++. The matter of the fact is if in C# they serve mostly as an alternative to interfaces and polymorphism. Since I can create subclasses of a specific class and supply them the appropriate methods to each one, what offer delegates additionally to that?

Dynamic function return type

隐身守侯 提交于 2019-12-20 07:58:53
问题 I have an app separated on modules. There are several Entities and CSV module. Csv module supports only struct(Entity), but i want to make CSV module works with any type of entity. Now it works like this: Csv module receives data from channel and strictly converts it to EverySize struct. How can I achieve dynamical return type, so it will works with any type of Entity, not only with Everysize func prepareWrapData(data []feed.WrapExporterChannels) []everysize.EverySizeItem { var result [

exception handling in the implemented method

天涯浪子 提交于 2019-12-20 06:29:04
问题 The code below gives a checked error to throws Exception : import java.io.IOException; interface some { void ss99() throws IOException; } public class SQL2 implements some { @Override public void ss99 () throws Exception {} // ... } while the one below compiles fine: import java.io.IOException; interface some { void ss99() throws IOException; } public class SQL2 implements some { @Override public void ss99 () throws NullPointerException {} // ... } On what logic is Java doing this-- any ideas

Creating an “object” of an interface

半城伤御伤魂 提交于 2019-12-20 06:13:43
问题 Today I had a bit of an argument with a friend who claimed that an interface object can be created. When I said that it's impossible, he showed me the following piece of code, which seemed similar to anonymous classes.Now the question is, what's the right answer? public interface I { public void f(); } public class InterfaceTest { public static void main(String []args){ new I(){ @Override public void f() { System.out.println("HELLO"); } }; } } Can this really be called creating an interface

How can a delegate & interface be used interchangeably?

无人久伴 提交于 2019-12-20 05:15:36
问题 Can I use interface method instead of delegate? How? I found searching that interface method is faster than using delegates. I would appreciate a simple code snippet. 回答1: In theory, it's possible to accomplish everything done by delegates with interfaces (Java has no delegates, for instance) containing a single method. However, it makes the code more verbose and adds little benefit. Then again, it's possible to do everything without classes and possibly do it faster. That doesn't mean you

XML serialization of interfaces

风格不统一 提交于 2019-12-20 05:14:10
问题 I need to serialize complex objects in my project and put them in a database. I'd like to serialize them using XML for obtain a easier debugging of my application. My case is very similar to what is described in this article: http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx So I have an object containing a Property which type is defined by an interface. Then I have different concrete types implementing it. Following the