naim5am
naim5am

Reputation: 1374

how to find out if a certain namespace is used, and if so, where?

I am writing some class and it wont compile without "using System.Linq". But i don't understand why its needed. What i am writing has nothing to do with Linq. How can i find out where a namespace is used?
And the very badly written piece of code (I am trying to figure out what i want it to do):

using System;
using System.Collections;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace DateFilename
{
  public class FailedFieldsList
  {
    private static List<FailedFields> ErrorList = new List<FailedFields>();
    public void AddErrorList(FailedFields errs)
    {
        ErrorList.Add(errs);
    }
    public void addSingleFailedField(string vField, string vMessage) 
    {
        //FailedFields
    }
    public List<FailedFields> GetErrorList()
    {
        return ErrorList;
    }
    public class FailedFields
    {
        public List<FailedField> ListOfFailedFieldsInOneRecord = new List<FailedField>();
        public class FailedField
        {
            public string fieldName;
            public string message;
            public FailedField(string vField, string vMessage)
            {
                this.fieldName = vField;
                this.message = vMessage;
            }
            public override string ToString()
            {
                return fieldName + ", " + message;
            }
        }
        public void addFailedField(FailedField f)
        {
            ListOfFailedFieldsInOneRecord.Add(f);
        }
        public int getFailedFieldsCount()
        {
            return ListOfFailedFieldsInOneRecord.Count();
        }
    }

  }
}

Error message produced when i dont include the linq namespace:
Error 4 Non-invocable member 'System.Collections.Generic.List<DateFilename.FailedFieldsList.FailedFields.FailedField>.Count' cannot be used like a method. D:\Slabo\My Documents\Visual Studio 2008\Projects\DateFilename\DateFilename\FailedFieldsList.cs 47 54 DateFilename

Thanks

Upvotes: 0

Views: 2245

Answers (6)

Jason Haley
Jason Haley

Reputation: 3800

One quick way is to open it in ILDasm - it almost always shows the complete namespaces of each type and method in use in your code ... however you may perfer using Reflector so you can switch from IL to C# ... of course you'll need to get it to compile first :)

Upvotes: 0

Nick Monkman
Nick Monkman

Reputation: 637

A more general (and proactive) way is to use a tool like NDepend.

Upvotes: 0

Mark Carpenter
Mark Carpenter

Reputation: 17775

You could be using some Extension Methods included in the System.Linq namespace (like Distinct, Select, Where, or some other collection methods). The build output window should give you a pretty good indication where the necessary using statement should be.

EDIT:

'System.Collections.Generic.List<DateFilename.FailedFieldsList.FailedFields.FailedField>.Count cannot be used like a method.

The only place that contains the word count in your code sample is:

ListOfFailedFieldsInOneRecord.Count() in getFailedFieldsCount()

Count is an Extension Method introduced in the System.Linq namespace, namely via the Enumerable static class.

Here are all of the Extension Methods this class affords you.

Upvotes: 3

Asik
Asik

Reputation: 22133

The problem is in the last method:

public int getFailedFieldsCount() {
    return ListOfFailedFieldsInOneRecord.Count();
}

The method Count() is not a member of a List(T). The property Count, however, is. If you replace Count() by Count, this will compile without the need for using System.Linq.

By including System.Linq, you enable the extension method Count(), which, confusingly enough, does exactly the same thing.

See List(T) Members on msdn for a breakdown of what's part of a List(T).

Upvotes: 6

VoteyDisciple
VoteyDisciple

Reputation: 37803

A using declaration is never "needed," since you can always reference System.Linq.Foo explicitly wherever it is that you can reference Foo when you are using the namespace.

Hence, the fastest and easiest way to figure out where a namespace is used is to try compiling the program and seeing where any errors come up. If Foo is suddenly undefined, you've found the culprit!

Upvotes: 1

LorenVS
LorenVS

Reputation: 12857

How about checking where the compiler tells you your error is when you remove the "using System.Linq"???

Upvotes: 1

Related Questions