1

This is more of conceptual question, since I am new to C# trying to get a simplified understanding.

My is trying to read lines from text file, which works fine but I am trying to implement following solution to read the last line as well but it gives following error :

Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Last' and no extension method 'Last' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

Code:

using System;
using System.IO;
using System.Collections;

    class Program
    {          
        public static void Main()
        {
            int counter = 0;
            string line;
            string xml_files;
            // string jpg_files;
            System.IO.StreamReader file = new System.IO.StreamReader(@"C:\test\logfile_c#.txt");

            string lastLine = File.ReadLines(@"C:\test\logfile_c#.txt").Last();

            while ((line = file.ReadLine()) != null)
            {
                Console.WriteLine(line);
                Console.WriteLine(counter);
                Console.Read();
                counter++;
            }
        }
    }

Can someone help me understand better how to avoid this.

Thanks!!

greg
  • 1,289
  • 2
  • 14
  • 33
user2385057
  • 537
  • 3
  • 6
  • 21
  • an old post here (answers your question) answered by @John Skeet http://stackoverflow.com/questions/2659290/ienumerable-doesnt-have-count – Rahul May 16 '13 at 09:44

3 Answers3

4

You just need:

using System.Linq;

Last is an extension method in System.Linq.Enumerable, and extension methods are discovered via using directives, basically.

It's probably worth reading up about LINQ and all the bits of the language that come together to make it work, rather than learning little bits piecemeal.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

How about adding the following line at the top:

using System.Linq;
Chief Wiggum
  • 2,784
  • 2
  • 31
  • 44
1

You have to import System.Linq

using System.Linq;
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82