C# Interface as DataType [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Hi guys I'm trying to learn some interface. I got the basic knowledge about it, that is a contract for a class. I am a bit confused now about Interface being a data type.
Question:
What is the Purpose of using an Interface as a Data Type?
Ex.
IEnumerable Folders {get;}
or
IComparable Compare {get;}
If I'm correct then IEnumerable is also an Interface.
Where is the Value Type saved (if there is any)?

It means that the data type can reference any object of a class implementing the interface.
IEnumerable thing = new List(); works
IEnumerable thing = new Collection(); also works
IEnumerable thing = new HashSet(); also works
In any case, you don't need to know the exact type that is actually assigned to your object, you just want any type that implements the contract
For example, all IEnumerator implement GetEnumerator() which allow you to use foreach
foreach (var elem in thing) will work whatever type is thing, it just needs to implement IEnumerator.

Related

Should we declare a collection class or interface? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I've got this idea from Java - i was told that you should declare a collection like this
List<Object> myList = new ArrayList<Object>();
where ArrayList is a class implementing List interface. The point of this is to enhance maintainability by generalizing code - as, should you change teh implementation to, e.g LinkedList<Object>, you could to it 100% painlessly.
So, projecting this on C#, is it considered a good practice to do the same thing in C# :
IList<Object> list = new List<Object>()
?
EDIT : i just found that LinkedList in C# does not even implement the IList interface, so i guess it settles the question for lists at least
Yes, generally this is a good practice to use interfaces wherever you can.
There are two important exceptions for containers, though:
When you must use a hash-based container for objects that are not comparable, use HashSet<T> or Dictionary<TK,TV>,
When you declare a local variable, using var for implicit typing is often preferred for convenience,
Note that if you plan to use a sorted container you have IOrderedSet<T> and IOrderedDictionary<TK,TV>.

C# Query WindowCollection [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm having elementary problem with trying to query System.Windows.WindowCollection at some point in my code i have
WindowCollection z = Application.Current.Windows;
and would like to do z.Any();
The definition of WindowCollection Class is as follows
public sealed class WindowCollection : ICollection, IEnumerable
As you may see, it doesn't implement IEnumerable<Window>, so in order to have access to the most of the Enumerable extension methods, you need first to use Enumerable.Cast like this
z.Cast<Window>().Any();
LINQ works only with IEnumerable<T> interface. WindowCollection impliments only IEnumerable. There two options:
Cast<T>() - this return IEnumerable but if collection has element which can't be casted to T exception will thrown.
OfType<T>() - this returns IEnumerable. It skips element which can't be casted to T, that is why I prefer OfType.
Application.Current.Windows.OfType<Window>().Any();

why microsoft allowed list<object> = new list<object>() feature from 2.0? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been learning collection and generics and understood that generics were introduced to
provide more type safe containers and to improve the application performance by reducing number of boxing/unboxing.
But, If that was the case, why does the framework allows to create generics collection of type system.objects which can take any other type?
List<object> listObj = new List<object>();
listObj.Add(new Program1());
listObj.Add(new Program2());
Thanks in advance.
Yes the collection is List<T> is object not a type? object is a type like any other (being the base type doesn't make it any different) and therefor can be used as the type argument for generic collections, there is nothing wrong with that, just don't do it when you know what the specific type is.
I would say: why not. A list of objects, in which form you want it is totally legit. I personally don't want the framework to tell me what to do. It is fine to do suggestions, but prevent this code? No.
If you want a list of things that are neither the same type, nor share a base class, nor share an interface, then you need List<Object>.
You very rarely need this.
But if you pursue the thought that List<Object> should be disallowed because it encourages unnecessary and dangerous downcasts, and anyway with generics you can just specify the types actually can handle, shouldn't you then go all the way and disallow the use of Object entirely?

Polymorphism in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a problem with a class that is to spawn instances containing one out of about 30 objects of different types (I call it out_out_of_many_types_of_subcontract in the code snippet below).
class supercontract
{
void supercontract (float date, one_out_of_many_types_of_subcontract subcontract)
{
stuff....
}
}
Is there any way of declaring a semi-generic variable or must I (1) resort to polymorphism between constructors or (2) casting an object as a certain type with a block of (else)if clauses?
Cheers!
I would make them all implement a single interface.
interface IContract{}
class AContract: IContract {...}
Even if the interface is empty you can limit what types could be passed to your method.

Marker interface in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently trying to implement a marker interface in c#. But I haven't done this so far so I'd like to ask you for advice please.
Short my problem:
I've a interface Factory, and the factory contains several subinterface which should be marker interface because they don't have a property or method or something. So the marker interface should implement the interface factory. And my marker interface is for example for Cars, Trucks etc. And the marker interface is implemented for Cars for MyBMW, MyAUDI etc. How can I implement such a pattern? Thanks!
interface Factory
{
string[] process (string [] entry);
}
You should think about using Attributes instead of interface to mark classes.
then you can decide in your code how to handle specific instances as the attribute is set.
I believe you want to implent the factory pattern. I suggest you to look an example here:
http://en.wikipedia.org/wiki/Factory_method_pattern

Categories