I don't understand why sometime I can't perform a .where() on a generic list. Most of the time it's shown in the intellisense, but sometimes it's not.
Is it related to the fact that the list is a parameter? Or is it related to resharper's intellisense?
You need to import the appropriate namespace:
using System.Linq;
You are missing
using System.Linq;
Without that, LINQ extension methods will not show up.
Related
It seems like there are methods and properties of ObservableCollection that are missing. I'm so confused.
Here is a screenshot of an old project:
And now my current project:
This is the using statement in both projects:
using System.Collections.ObjectModel;
Did they update the SDK and removed all of the methods or am I using the wrong ObservableCollection? I have no idea what's going on.
When you see the down arrow on a method that means that method is not part of the class but is a "Extension Method". Extension methods are basically static methods from other classes that act like instance methods on the class you are working with.
Most of the items in your list are from adding using System.Linq; to the top of your file. This causes all of the extension methods in System.Linq.Enumerable to show up in the list, this will give you things like All<> or Any<>. However AddRange<> is not a standard extension method in System.Linq and may be added by some other 3rd party library (or some namespace in .NET I am not aware of) that you are using.
The easiest way to find out where you are getting AddRange<> from is go to the project that it works for and then right click on the method in code and you should see a "Go To Definition" or similar1, that should take you to the file or the metadata view of the file that declared that extension method.
1: I use Resharper and it changes my right click menu so I don't know the exact wording
I think you're missing the LINQ extensions. Try adding:
using System.Linq;
The "missing" methods are LINQ extension methods. Add LINQ to your list of included libraries.
The first statement of all my C# files is "using System;".
Now with framework version 4 this namespace contains a class called "Action". This is also the name for a class im my own code in a regularly used namespace. Now there is of course a conflict. Ofcourse I can resolve this conflict by using explicit "MyNamespace.Action" where ever I was using "Action" before. This are several hundreds if not thousands of lines. Or I could not use the System namespace, which of course leads to many other problems. I would like to write something like "using System but not System.Action", but I cannot find a statement to do this.
Any ideas?
No, you can't.
But you can add using Action = MyNamespace.Action. This will be highly confusing for new developers, though, as Action is a fundamental part of .net since 3.5 so I strongly suggest you rename your class.
The using directive has two uses:
To permit the use of types in a namespace so you do not have to qualify the use of a type in that namespace:
using System.Text;
To create an alias for a namespace or a type (you could go for this one).
using Project = PC.MyCompany.Project;
The using keyword can also be used to create using statements, which define when an object will be disposed. See using statement for more information.
using directive (C# Reference)
Almost all C# files have using statements at the top of the page
i.e.
using System;
using System.IO;
//code....
What do the using statements mean that are at the top of the page? Why is the syntax different from other using statement declarations.
i.e.
using (ResourceType resource = expression) statement
Those are using directives. They tell the compiler which namespaces to look in to find the classes you use in your code.
They look different (and are completely different) from the using statement which defines a scope for disposable objects.
These statements tellt he compiler which namespaces to look in to find the classes you are using in the code.
For example if you have
using System.IO;
Then your code to read all text of a file can be
File.ReadAllText("MyFile.txt");
rather than
System.IO.File.ReadAllText("MyFile.txt");
The using directive (as opposed to the using statement you mention that handles disposable objects) allow you to not specify the whole namespace of a class
i.e. if there is a class called
System.IO.FileStream
Then you could put
using System.IO;
And refer to it as
FileStream
(as long as the compiler can only determine a single thing that might mean)
using is a contextual keyword; it has more than one meaning, depending on how it's used.
At the head of a .cs file, it works like the java import instruction, specifying namespaces to search when looking for a type. If it's not listed, you must fully qualify types you use, which gets cumbersome. However, importing namespaes you don't need is wasteful and can introduce ambiguities.
Check out http://msdn.microsoft.com/en-ca/library/zhdeatwt(v=VS.80).aspx
The using keyword has two major uses:
As a directive, when it is used to
create an alias for a namespace or to
import types defined in other
namespaces. See using Directive.
As a statement, when it defines a
scope at the end of which an object
will be disposed. See using Statement.
1) The using keyword followed by a resource path provides a reference to a library in order to use additional/special classes and methods. This is similar to import-like keywords in other languages.
2) The using statement obtains the resource specified, executes the statements and disposes the object (releases from memory).
The kind of usings that exists are the
using directive, the one on top of files. It has two versions
using System.Text; - search this namespace for types that are not given by a fully qualified name. Similar to the %PATH% system variable.
using Project = PC.MyCompany.Project; - an "alias" for a type or namespace
The other kind of using is the using statement, the one with using (var foo = IDisposable){...}
This is a shortcut for a try-catch-block that calls Dispose on the foo-variable at the end.
In a specific project at my work, I have a method that returns IList. But this interface does not contain where, or FindAll filters. However, when I open a new project, IList contains all. What is the difference?
Did you import System.Linq ?
Nope. IEnumerable<T> has "where" as an extension method.
Assuming your project is .Net 3.5 or greater, you need to have using System.Linq;
You might find this useful: LINQ, Where() vs FindAll()
Check .NET Framework of opened framework, may be its .NET Fx 2.
System.Linq added in 3.5
Here's a basic discussion of extension methods in general. As mentioned by others, the Where method is an extension method found in the System.Linq namespace so you need to import it in order to have intellisense detect the existence of those methods.
For example:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Security;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Web.Configuration;
using System.Collections.Generic;
I have something like that in a class that I have inherited. I could remove each namespace one by one to see if the class builds, BUT does it matter?
If I am not sure if System.Xml is needed, should I remove it?
In general all they do is add clutter.
However, they can in some specific circumstances cause problems with duplicate names in scope, or duplicate extension methods. For example, if there are classes Foo.SomeType and Bar.SomeType, and you have using Foo; and using Bar;, then references to SomeType will need disambiguating. The extension methods work similarly where static classes in different namespaces contribute conflicting extension methods.
The VS2008 tools (including express) include options to remove unnecessary using statements, which is very useful for tidying the code.
In general I like to remove unused using statements as they clutter up the code.
ReSharper grays out unused using statements.
It has a right click menu for, "Remove unused using statements" also.
Using statements are only used by the compiler for type resolution so feel free to remove them if they are not needed. Having extra will not hurt performance and removing them will only slightly improve compilation time.
If classes with simular names exists in different namespaces it could make it harder to follow the code.
The unused ones will be removed on compile time so it makes no difference, don't worry.
Here is probably the definitive post on performance of using statements: Do namespace using directives affect Assembly Loading?
The only harm it could cause would be if two of the namespaces happened to have a class of the exact same name within it: but at that point, the IDE would warn you.
Code tools will tell you if they are superfluous. I would just leave them. They don't do any harm. (Resharper for instance)
In the past when I manually hand removed unused INCLUDEs, it had no effect on memory or speed.
As others have mentioned here, there could be collision issues but if they're all .NET classes, I wouldn't worry too much about it.