Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, objectCollection.OfType<T>() and foreach (T item in objectCollection), IEnumerable to IEnumerable<T>

To my personal coding style belongs Enumerable.OfType<T>(). I use it everywhere it makes a little bit of sense. Especially IEnumerable<T> allows mighty linqToObject functionallity. I hate the "type-unsafe" looping of ObjectCollections such as the samples below. Now I have some questions about looping across these "UnGenericCollections".

  • ConfigurationSectionCollection
  • HttpModuleCollection
  • SPBaseCollection
  • SPFieldCollection
  • ArrayList
  • so on...

Question 1: If I convert this ArrayList to Enumerable<T> how big is the additional loop in comparison with the simple foreach/if-checks?

var ark = new ArrayList();
ark.Add(new Human());
ark.Add(new Human());
ark.Add(new Animal());

Instead of:

foreach (object passenger in ark)
{
    if (passanger is Human) { }
    if (passanger is Animal) { }
}

I use:

foreach (var human in ark.OfType<Human>())
{
}

foreach (var animal in ark.OfType<Animal>())
{
}

Question 2: During a foreach loop into a different typed variable, which way of casting/converting will be used? Is this a language feature or does that work out of the box?

foreach (Human human in ark) { }

Thanks for enduring my awful English. Best regards,Benjamin

like image 525
benwasd Avatar asked May 19 '11 18:05

benwasd


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Ans 1:

In your sample - you may actually be iterating over the FULL enumerable twice.

// i use
foreach (var human in ark.OfType<Human>())
{
}

foreach (var animal in ark.OfType<Animal>())
{
}

Ans 2:

It would throw an InvalidCastException exception if there are any non-human in ark.

I would personally prefer ark.OfTypes<T>(), in case I know I only want to deal with Human and Animals but would be ignoring Elves. This way code is much more cleaner and you are dealing with strongly typed object in your foreach loop.

But again in case I do not want to ignore Elves, I would take rather iterate thru full ArrayList and use casts.

like image 197
YetAnotherUser Avatar answered Sep 24 '22 02:09

YetAnotherUser


You'll find no difference since ArrayList implements IEnumerable. Actually, anything that implements this interface may be used in a foreach statement.

Casting the object instead of setting up a var will build (since it's an explicit cast) but if you have an animal inside the enumeration, you'll end up having to deal with casting exceptions.

foreach (Animal animal in ark) { } // this will blow up an exception if the object is Human
like image 43
viniciushana Avatar answered Sep 27 '22 02:09

viniciushana