Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# List Generic Extension Method vs Non-Generic

This is a simple question (I hope), there are generic and non-generic methods in collection classes like List<T> that have methods such as Where and Where<T>.

Example:

        List<int> numbers = new List<int>()
        {
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10
        };

        IEnumerable<int> evens = numbers.Where((x) =>
        {
            return x % 2 == 0;
        });

        IEnumerable<int> evens2 = numbers.Where<int>((x) =>
        {
            return x % 2 == 0;
        });

Why use one over the other (Generic or Non-Generic)?

like image 661
infbubble Avatar asked Mar 01 '13 07:03

infbubble


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

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.

Why is C programming language important?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.

Why is C called a mid level programming language?

C has the features of both assembly level languages i.e low-level languages and higher level languages. So that's why C is generally called as a middle-level Language. The user uses C language for writing an operating system and generates menu driven customer billing system.


1 Answers

They're the same method (documentation here). The type parameter portion after the method name (i.e. <int> in this case) is optional when the compiler is able to infer the type automatically and unambiguously from context. In this case, the method is being applied to an object implementing the interface IEnumerable<int> (i.e. the object numbers of type List<int>) from which the compiler can safely infer that the type parameter is int.

Note, also, that Where<T> is actually an extension method on the System.Linq.Enumerable class which can be applied to objects of any class implementing IEnumerable<T> such as List<T>.

like image 145
Richard Cook Avatar answered Sep 19 '22 15:09

Richard Cook