TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# AsEnumerable Method

Use the AsEnumerable extension method from System.Linq to cast to IEnumerable.
AsEnumerable. The IEnumerable interface is a generic interface. This means it defines a template that types can implement for looping. The AsEnumerable method is a generic method. It allows you to cast a specific type to its IEnumerable equivalent.LINQ
Example. We use AsEnumerable on an int array. AsEnumerable is an extension method and you must include the System.Linq namespace to access it. When you have an IEnumerable collection, you typically access it through queries or a foreach-loop.Int ArrayExtension

Next: The example code also shows the implementation of AsEnumerable in the .NET Framework. This only casts the parameter and returns it.

C# program that uses AsEnumerable method using System; using System.Linq; class Program { static void Main() { // Create an array type. int[] array = new int[2]; array[0] = 5; array[1] = 6; // Call AsEnumerable method. var query = array.AsEnumerable(); foreach (var element in query) { Console.WriteLine(element); } } } Output 5 6 Implementation of AsEnumerable: .NET public static IEnumerable<TSource> AsEnumerable<TSource>( this IEnumerable<TSource> source) { return source; }
You can see that the value returned by the AsEnumerable method invocation is typed with the implicit type var. This makes the syntax clearer. You could explicitly specify the type as IEnumerable<int> instead.Var
Summary. The IEnumerable interface in the C# language provides a contract that allows a typed collection to be looped over. More specific, physical collections such as arrays or Lists can be used as IEnumerable collections.

And: To acquire a reference from one of those types to a simple IEnumerable type, you can invoke the AsEnumerable extension method.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf