C # using pointer depth

I am trying to understand the C # directive using ... Why does this work ...

using System; using System.ComponentModel.DataAnnotations; namespace BusinessRuleDemo { class MyBusinessClass { [Required] public string SomeRequiredProperty { get; set; } } } 

but is it not so?

 using System; using System.ComponentModel; namespace BusinessRuleDemo { class MyBusinessClass { [DataAnnotations.Required] public string SomeRequiredProperty { get; set; } } } 

The second leads to a compilation error "The type or namespace DataAnnotations cannot be found. Are you missing the using directive or assembly references?

+8
c #
source share
2 answers

using the directive allows you to use class names from the namespace that you specified as an argument. Since DataAnnotations is not a class, but a namespace, it is not available in the second case. You must either use the fully qulified class names NS1.NS2.Class1, or run the program using NS1.NS2; and then use Class1.

+7
source share

Because you can use Fully Qualified Class Names or just use ClassName and allow .NET to resolve it using the using directive. There is no middle place.

Class1 -

 NameSpace1.NameSpace2.NameSpace3.Class1 

can access

 using NameSpace1.NameSpace2.NameSpace3; ... ... Class1.DoSomething(); ... 

or

 ... NameSpace1.NameSpace2.NameSpace3.Class1.DoSomething(); ... 

but don't like

 using NameSpace1.NameSpace2; ... NameSpace3.Class1.DoSomething(); ... 

In the latter case, what the compiler is really looking for is the root NameSpace3 namespace, which clearly does not exist. He will not try to search for System.NameSpace3 or NameSpace1.NameSpace3 or NameSpace1.NameSpace2.NameSpace3 etc. Etc.

+1
source share

All Articles