Import all subclasses like Java but in C# - c#

This is such a dumb question, but I can't figure out the lingo to ask Google.
In Java if I wanted to import all subclasses I would use something like
java.util.*
And all of util would be imported.
Firstly, what is the proper lingo for what I'm doing in C# so I can start using Google more effectively. Am I importing namespaces? Libraries? Subclasses? (Can you tell I'm new at this?)
Secondly, since I'm here, how is this accomplished in C#?
PS- I did click on every related question stackOverflow threw at me to see if the answer would pop up. No luck. I'm simply without words to describe what I'm looking for. The example should do just fine but... Anyone who can take a moment to either explain the lingo to me or perhaps simply point me at something that can (in a nutshell, I have a couple books for the long haul) that would be great.

Firstly, let's differentiate between assembly references and namespaces.
Assemblies are what you add references to in your c# project, they are the libraries that contain the actual classes you need, usually found as DLL files. The .net framework contains many such assemblies, and Visual Studio will try to reference the most commonly used ones in your project (e.g. for a WinForms project it will automatically add a reference to System.Drawing.dll).
Namespaces are logical partitions of the classes in an assembly.
Once you reference an assembly in the project, all classes in all namespaces are available for use, if you provide their full name.
This is where the using directive comes in.
It is simply syntactic sugar for not having to write very long names all the time.
For example, assuming your project references the System.Drawing.dll assembly, you would have to qualify a class from this assembly using it's full name, for example
System.Drawing.Imaging.BitmapData
Because this is tiresome and bloats the code, if you start your .cs file with
using System.Drawing.Imaging;
you could then instantiate a class using just the name BitmapData.
This will be true only for the .cs file in which you added the using directive, not for the whole project.
Also, it's important to note that using one namespace does not bring in all nested namespaces, you have to using each one individually.

You must brush up on your your Google-fu
http://www.harding.edu/fmccown/java_csharp_comparison.html#namespaces

It can be called importing / referencing/ using namespace.
Such a language feature is not available in c#.
A little explanation: Namesspaces can be spread across multiple libraries. so when you use a namespace it may refer to it from multiple referenced assemblies.

It's called namespace and it's imported by a keyword using. For example:
using System;
This statement enables you to reference all the classes that exist in that namespace. They, however don't enable you to reference any class in the subnamespace of declared namespace. You have to declare each namespace separately. For example:
using System;
using System.Text;
Of course, you need to have a proper references added to the project where you're specifying the using directive.

Within .Net, you first would need to ensure that there is a referenced assembly containing the namespace you would like to import. Once that reference exists, you can use the 'using' directive to bring that namespace into the class so as to not have to fully qualify all object names. You can find more information on this directive on MSDN.
If I misunderstood, let me know and I'll do my best to get you pointed in the right direction.

Related

How do I find what reference file a namespace in a using directive is coming from?

I have some older software that I am maintaining. I am attempting to find out what specific reference file contains the namespace that is being brought into the project by the using directive.
Is there a simple way of finding which reference file contains the namespace of a given using directive? This seems like it should be easy, and it probably is, but all of my searching using these terms returns results related to how to implement using directives and reference classes, etc. However, I cannot find an answer for how to find the reference containing the namespace in question.
For some reason, the references often contain namespaces that have nothing to do with the name of the file. For example, I've seen references like Foo.cs that contain the namespace Bar. I then see
using Bar;
and can't find an easy way to see what reference file contains the namespace.
Anyway, apologies in advance if this is something stupidly simple that I'm just not finding.
Object Explorer? It will show the assembly name – Fals
I was able to find the namespace under the type in the object explorer, thank you. Even as a relative beginner, this is something I should have known to try, so thank you for taking the time to point me in the right direction.

How to make the C# namespace work like Java Packages so they rename automatically when moving them?

I come from Java and see that package in Java is very convenient. When you move a class to another package, it will change automatically the package. (of course, by IDE such as Eclipse or Netbean)
But C# is using namespace and don't have my namespace renamed automatically like it does in Java. For example I have a file which namespace is com.app and I put it in com.app, but at later time, I move this file to com.lib folder and its namespace still be com.app. So, I find this is difficult to manage because I'm moving it manually.
Please give me help in how to fix my problem. (that namespace of file is named by folder it contains, and when I move to other, I will automatically change). Can we do it?
I fix the problem by using an IDE plugin called Resharper. (Among many, many useful features) it highlights when a namespace is wrong (based on the folder hierarchy and root namespace of the assembly) and can fix it for you.
Note that unlike in Java, there are sometimes very valid reasons for a class to be in a namespace other than the one inferred by the directory structure. A good example might be extension method classes, which need to be in scope in the class that is invoking them. Therefore it is common to have:
/myProject
/extensions
/MyExtensionMethodClass.cs
with a namespace like myProject (so that the extension methods can be used anywhere in myProject without a using directive)
Thats actually because C# has the concept of partial classes , that is , you can distribute your C# class along several files instead of just having it coded into a single file , like Java. For that reason , namespaces in .Net are distributed containers instead of centralized containers , defined by your namespace orperator.

What does the using directive do, exactly?

On MSDN I can read what it does, but I would like to know what it does technically (tells compiler where to look for types..)? I mean using as a directive.
The primary function of the using directive is to make types within a namespace available without qualification to the user code. It considers the set of namespaces and types which are defined in referenced assemblies and the project being compiled.
Take for example the following definition in MyTypes.Dll
namespace MyTypes {
class Class1 {}
}
Now consider referencing MyTypes.dll from another project with a different namespace. Without a using directive to create Class1 i need to qualify the name
MyTypes.Class1 local1 = new MyTypes.Class1();
The using directive lets me remove this qualification
using MyTypes;
...
Class1 local1 = new Class1();
#JaredPar's answer is correct, however I'd like to add that it doesn't quite work the same way as say import in Java. (someone correct me if I'm wrong about Java's import actually importing it into memory)
You need to include either a DLL or project reference in order to even be able to use using however it's not loaded into memory until you actually call into a method/property/something in the assembly. So you could have using System.Linq; but if you don't actually use any Linq methods, the Linq assembly is never loaded. (I'm not 100% positive that Linq is in it's own physical assembly since namespaces and assemblies aren't 1:1, but for sake of example I'm assuming it is)
using informs the compiler which namespaces to search for names used in the file but not defined in the file.
using just tell the compiler to go to the assembly's configuration file and search for a specific DLL with the name given, if the DLL is found then it links that dll into the current project. using is just a link operation to have DLL talk to each other in a shared space in memory.
The guy below here is right

Why do I have to add System.Data.Entity to use System.Data.Objects?

I am still learning about programming as you can probably tell by my question.
How come I have to add System.Data.Entity as a reference to my project to use System.Data.Objects? Does Objects live in namespace System.Data.Objects? Wouldnt it make sense for Objects to live in System.Data.Entity.Objects?
The .NET namespaces are cross-assembly. This allows the library designers to expand particular namespaces as appropriate without polluting core libraries with non-core functionality. The naming of individual DLLs is unfortunate, but it is not intended to reflect namespace information in the way you're thinking.
In your example, System.Data.Entity is a DLL containing elements from a number of different namespaces. One of these is System.Data.Objects, as you've discovered.
Namespaces and assemblies are entirely separate concepts. Sometimes - heck, often they will match up, but they certainly don't have to. You don't tend to use the mscorlib namespace, for example :) Likewise most of the System.Linq types are in System.Core.dll. One assembly can contain types in multiple namespaces, and multiple assemblies can contribute to the same namespace.
It's worth keeping the two concepts as distinct in your mind as possible. Fortunately it's easy to find out where a type "lives" in both respects from MSDN.
The System.Data.Objects namespace is defined in System.Data.Entity.dll.
The assembly is called System.Data.Entity which means the DLL System.Data.Entity.dll.
System.Data.Objects is a namespace within that assembly.
A single assembly may contain one or more namespaces possibly all with different names.

Namespace and Assembly names for a reusable component

I've got a bit of a conundrum on my hands.
I'm currently compiling a load of 'code snippets' into reusable libraries that I obviously intend to use in multiple applications.
I'm having trouble deciding on an appropriate namespace and assembly name.
Basically, I've currently got JasonSummers.Validation as an example for my validation library, since I have no 'company' and no specific project which the code applies to.
However, later on, when I come to use said namespace in a client's project, I don't think it's really appropriate to have my name referenced in code which they will probably own the IPR for.
I would just use 'Validation' as the namespace (after all, StructureMap is StructureMap, not JeremyMiller.StructureMap) but this may cause confusion for future developers as 'Validation' is used in the 'System' set of namespaces provided by .net
All comments greatly appreciated.
You can always use name from biology e.g Tribulus. or any other for your root namespace. So your code goes into e.g Tribulus.Validation or Tribulus.Utilities etc. Toplevel namespace need not to be a functional name. It can be just a signature of a company or just a unique interesting name as i mention.
In my personal experience, I maintain a code base for that useful functions at source level, i.e., I copy every function I need in every project, under my client brand and assembly name.
I didn't found be useful to keep that functions at assembly level, exactly because that: it'll contain some names which can generate confusion and for an extra reason: a client paid for some functionality, but not for another (include in a general assembly). So, I just pack what he/she bought.
An option could be to use a neutral name, like Reusable and to merge your utility assembly by using ilmerge framework command.
Take a look at Microsoft's Namespace Naming Guidelines
I got it as an answer from Konamiman to my question which is related to yours.

Categories

Resources