C# Query WindowCollection [closed] - c#

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();

Related

C# Interface as DataType [closed]

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.

using params[] wih ObjectDataSource [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
My EF insert method is defined as follows:
public void Add(params T[] items)
How can it be used with ObjectDataSource to insert objects?
According to the documentation, the insert method for an ObjectDataSource is designed to call a method that has parameters for each value of the item being inserted, not the item itself (let alone an array of items).
I would either add an overload to your repository that accepts the value for a single item (and perhaps calls Add), or add a mapper somewhere that maps the values to a new item and calls your Add method.

How to return value of List without []? [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
Is it possible to return value from list or a array without brackets?
For example i have List of integers:
List<int> myIntegers = new List<int>();
myIntegers.Add(1);
myIntegers.Add(2);
myIntegers.Add(3);
int newValue = myIntegers; //normally i should use myIntegers[0]
i want myIntegers to return the first value from list but i want to use it without brackets. Is there a way?
Thanks. M.
As you wrote it, it will never compile. However, if you really have a thing against [], then yes. You can use First() (or its companion, FirstOrDefault()), or just ElementAt(0).
First and FirstOrDefault can take a predicate, which is often useful, FirstOrDefault returns default(T) if there is no first element that matches the predicate, and ElementAt does the exact same thing as the [].
If you subclass List<T>, you could potentially do this with an implicit conversion to T. Although, what you are describing is sort of a weird data structure, and not really idiomatic c#. Probably best not to call it a list.

Interface in C# what does it mean in this context [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
Was going through this code here
The code here confused me
private void sendMessage()
{
IConnectionFactory wlsConnectionFactory;
IQueue ordersQueue;
IDictionary<string, Object> environment;
IContext jndiContext;
In dot net and C# a I prefix means a Interface no ?
If the above statement is true what does this line mean for example
IQueue ordersQueue;
Would this program have worked if we had something like
Queue ordersQueue instead of IQueue ordersQueue ?
First of all, an I prefix implies an interface, but that's just a convention (albeit, a extreme widely-used convention)
IQueue ordersQueue;
means that ordersQueue can be assign any type which implements the IQueue interface.

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.

Categories