3

In my C# Project, I am getting this error that the definition for count does not contain System.Array

protected void Page_Prerender(object sender, EventArgs e)
{
    FileInfo[] fileInfo;
    string UpFolder = Server.MapPath("~/data/images/");
    DirectoryInfo dir = new DirectoryInfo(UpFolder);
    fileInfo = dir.GetFiles();
    // here i am getting error
    int TotalImages = fileInfo.Count();
    for (int count = 0; count < TotalImages; count++)
    {
        Image image = new Image();
        image.ImageUrl = "~/data/images/" + fileInfo[count].ToString();
        image.ID = count.ToString();
        DataListImage.Controls.Add(image);
    }
    DataListImage.DataSource = dir.GetFiles();
    DataListImage.DataBind();

    string UpFolder1 = Server.MapPath("~/data/thumbnails/");
    DirectoryInfo dir1 = new DirectoryInfo(UpFolder1);
    DataListthumbnails.DataSource = dir1.GetFiles();
    DataListthumbnails.DataBind();
}
Karl Anderson
  • 33,426
  • 12
  • 62
  • 78
Siddiq Baig
  • 546
  • 2
  • 14
  • 33

2 Answers2

22

You don't need Count, instead you can use Length with arrays to get the count.

You are getting the error since you are missing using System.Linq;

To get TotalImages you can use

int TotalImages = fileInfo.Length;
Habib
  • 205,061
  • 27
  • 376
  • 407
  • i was using System.Linq already but why cant i use count here??? since there are some difference between count and length As i am resizing the image on the behalf of count images.. as much as images size it will be Optimized accordingly – Siddiq Baig Nov 04 '13 at 21:27
  • @SiddiqBaig, `Count` would return total number of elements in an your array, same as lenght, It doesn't has anything to do with Image Size. See http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection for Length Vs Count. – Habib Nov 04 '13 at 21:29
4

While Count() isn't "needed" since the type is an Array, the error is a result of not "using" the correct namespaces.

Enumerable.Count() is a LINQ extension method in the System.Linq namespace. As such, it can be made available with the following:

using System.Linq;

Since arrays implement IEnumerable<T>, then they can utilize any such methods when they are made available.

user2864740
  • 54,112
  • 10
  • 112
  • 187