DataAnnotations missing attributes - c#

I have using System.ComponentModel.DataAnnotations; at the top of the file, but essentially the only attributes I have are [DisplayName] and [Readonly]. Even [Display(Name...] does not exist. What am I missing?

Tools like Resharper allow you to analyze the using statements in a code file.
If you find that you do not need the using statement, remove it.

Use the following namespaces:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

Related

Encapsulate using directive statements C# [duplicate]

This question already has answers here:
Duplicated using directives in multiple files
(2 answers)
Closed 2 years ago.
I have a number of using directives declaring the namespaces I am going to use in multiple classes.
Is there a way to refer multiple using statements via a single using directive or any other approach to this solution that I can implement for all the classes?
The classes/files might or might not be under the same namespace.
Example :
//Fil1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
namespace NS1
{
public class1
{
}
}
The reason to encapsulate these using statements is because they'll be common among multiple files.
//Fil2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
namespace NotNS1
{
public class2
{
}
}
Would prefer if there were some way to just make one using directive call in the class and define all these using statements elsewhere. Thanks.
Theres no magic using or #include like in the C world. You have to be explicit in each file or namespace what you are using.
Many tools, including Visual Studio, ReSharper and Rider include tools to help manage using directives.
No, but IDEs usually handle imports quite well, but you can place all your classes in a single namespace and import that, even tho thats very bad practice.

Can "using <namespace>;" directives be made more efficient in .NET?

When I create a new class file in C#, the usual structure is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnitTest
{
class Class1
{
}
}
StyleCop doesn't like having using directives outside of the namespace, so ideally, I would refactor my code as such:
namespace UnitTest
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Class1
{
}
}
From my days as a Java programmer, I was taught that it was better to import only the references that were necessary, rather than the entire package like so:
import foo.bar.MyObject;
instead of
import foo.bar.*;
I know that this can also be done in C#, with the added feature that you can use aliases for types (sometimes useful when working though native code):
using StringBuilder = System.Text.StringBuilder;
or when using aliases:
using HANDLE = System.IntPtr;
using HDC = System.IntPtr;
So my questions, regarding best practice and efficiency of using directives:
Is is more efficient to keep using statements inside a namespace, or is this purely a stylistic preference?
If is more efficient to only include the necessary items, as opposed to the entire namespace?
There are some fringe cases where it makes a difference but for the majority of cases it is just a stylistic preference.
The using statements just tell the compiler where it can locate the types. It has no influence on the runtime.
nope; stylistic preference, as long as it doesn't introduce any semantic changes; the cases where it changes the meaning are very rare (although there used to be a bug with the LINQ-to-SQL code generator that did care about where you put them - this is now fixed)
nope; unused directives are not used, and they don't massively impact the compiler
Note that having too many directives can cause ambiguities; for example there are a myriad of classes called Timer. For that reason it is worth keeping things tidy.

List<T> without Where in the intellisense

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.

C# using statement at the top of the code page

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.

Does it matter if I include more references and namespaces in a class

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.

Categories