I'm new in C# and I'm now coding with Visual Studio 2013.
I'm developing a project and it can be divided into several functions.
When I want to separate the code into different packages like what I did with Java, I find that Visual Studio only offer me to create a new project in the same solution.
So does C# have anything like package in java? So that I can place my source properly. Or the project itself is just like packages in java?
Thank you very much!
No,
C# has namespaces. You can normally wrap objects (classes, enums, structs, etc) in namespaces
namespace Whatever
{
public class ClassA{}
public struct StructA{}
}
Then to instantiate a ClassA do the following...
Whatever.ClassA _classA = new Whatever.ClassA();
You can also at a "using" directive so you don't have use the namespace all the time in your code file....
using Whatever;
ClassA _classA = new ClassA();
Hope it helps,
Leo
As MSDN state:
The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.
Instead of package c# using namespace.
for java you use the jar file here dll.using the namespace you can access the dll's.
you can separate the class file using the folder also.you need to create the folder under the project.
I think you are trying to separate some sort of code from other.
you can use namespaces for doing that.
please find below link.
http://msdn.microsoft.com/en-us/library/z2kcy19k.aspx
Related
A beginner's question. It is c#.
Let's say I have three classes in one project named Employee, Department, Address. For some reason, I would like to have a .dll file (let's name it test.dll) to have all three classes included that I can call it from some other project using syntax like "test.Employee emp1 = new test.Employee();"
That is my idea. Is this possible? If yes, how should I do that? Do I have to create a class library project to do so? I know nothing about a class library project. So I may need further help with that.
If the answer is no, how do I add references to those classes from other solutions?
Thanks.
Create a class library project.
Give it a proper namespace
Write your library classes, then compile to a dll
Then add a reference to that dll in the other project you want to use it in
add a using statement to include the reference in your code files.
Pretty straightforward
I am brand new to Visual Studio. I have been coding in Java for many years but have taken on a project which requires me to use c# and visual studio 2012.
What I need to know is how to utilize a different SDK. I want to use something called Honeywell SDK instead of Visual Studios inherent SDK but I cannot find out where to change this setting. If anyone has an answer that would be greatly appreciated!
as a Java developer you are probably used to imports and presumably understand how to use the import statement to import the classes in a namespace.
In C#, the first thing you must do is add a reference to the library containing the methods you require - this is normally done by right clicking your project in Solution Explorer, clicking add reference, and then selecting browse to browse to the location what is normally a DLL containing the library methods in question.
Once you have added a reference to your project, you can access the classes in the library either using a fully qualified name, e.g. to access the Thread class in .NET's System.Threading namespace for example, fully qualified use would be as follows:
System.Threading.Thread thread = new Thread();
Alternatively, you can put a using directive at the top of each file where you intend to use the client to avoid the need for the fully qualified name. For example:
using System.Threading;
Then in code, you can simply use the shortened version of the class name by itself:
Thread thread = new Thread();
As you can see, the using directive is effectively C#'s equivalent of Java's import directive. Note that to import all classes in a namespace you do not need the .* wild card at the end of the using directive as you do an equivalent Java import statement.
In practice, you may need to refer to the documentation you have to confirm what namespaces they use, and what files you need to add references to to use their libraries as this detail will be vendor specific. For more detail and a more thorough explanation of the using directive then the MSDN documentation is likely to be the most helpful source:
http://msdn.microsoft.com/en-gb/library/sf0df423%28v=vs.80%29.aspx
and:
http://msdn.microsoft.com/en-gb/library/z2kcy19k%28v=vs.80%29.aspx
There is no inherent SDK per-se in a .NET project, though normally references to the .NET framework and default using directives will be added. You will probably find these useful as they contain core functionality and the references normally added by default in a new project will provide you access to things such as collections and so forth.
One final note is that C# has a using statement, as well as the using directive, so if searching for additional information on the directive, be careful not to confuse it for the using statement.
Sorry if my question seems a little noobish but I can't find an answer. I would like to use DirectInput for my XNA game. I've read around and it seems this is the best way to get input from a gamepad other than a XBox 360 controller. The first step is to include the Microsoft.DirectX.DirectInput namespace however I don't quite know how. Simply putting it in at the beginning of my class doesn't work, visual C# doesn't recognize it.
This got me thinking should I download the DirectX SDK? There is alot of stuff in there and the file is big how do I just download the single namespace? Is this even possible? If I can download the single namespace where do I put the file?
Now you can see all the questions racking around in my brain. So how do I make visual C# recognize the Microsoft.DirectX.DirectInput namespace so I can use the methods, properties and all that good stuff?
Thanks!
You need to set a reference to the DLL containing the code library you'd like to use. Presumably that's part of the SDK, so yes, you should download it. Then, in Visual Studio
open the solution explorer
open the project where you want to use the library
right-click the references node for that project
choose "add reference"
navigate to and select the dll
Once you've added the reference successfully, VS should recognize the using directive (and any types you've used from the assembly).
Note that references are made to assemblies; assemblies are not the same as namespaces. Beginners often confuse the two. Types that are defined in different assemblies can be in the same namespace; the BCL has dozens of examples of this.
Furthermore, the "using" directive doesn't cause anything to be loaded or anything like that; it just allows you to specify types without fully qualifying the names. In fact, it's possible to code in C# without ever using a using directive. For example, the following code files are equivalent:
using System.Collections.Generic;
public static class Collector
{
public static ICollection<T> Collect(IEnumerable<T> enumerable)
{
return new List<T>(enumerable);
}
}
AND
public static class Collector
{
public static System.Collections.Generic.ICollection<T> Collect(System.Collections.Generic.IEnumerable<T> enumerable)
{
return new System.Collections.Generic.List<T>(enumerable);
}
}
Download the SDK. It will not be redistributed with your app.. but everything you need to dev DirectX apps will be included in there. When installed there will be new entries in the "Add Reference" dialog.
In order to leverage DirectX you will need to download the SDK.
It is simply not possible to download a part of it. Even if it was, I would never recommend doing that.
I want to force a root namespace on the contents of any .cs source files that don't have their contents wrapped in an explicit namespace. In other words I want to keep classes and other namespace-level structures out of the default namespace.
(Working inside a Windows .NET environment with Visual Studio)
In the following example, I want to force the Car class into the MotorIndustry namespace by default even though it has no explicit namespace coded.
Vehicle.cs (has namespace)
namespace MotorIndustry {
public class Vehicle {
// ...
}
}
Car.cs (no namespace/default)
public class Car : Vehicle {
//...
}
Is there a way to accomplish this "root namespace" modification behaviour through project settings in Visual Studio, AssemblyInfo.cs file or some other means?
My understanding is VB.NET has a feature like this but C# acts differently?
Some context about why am I asking this: I have hundreds of classes and the programmer forgot to wrap the namespace around some. When I reference the assembly from other projects it's polluting the default namespace with classes that end up causing some ambiguous situations.
Use ReSharper to move the classes into the proper namespace. In fact, version 5.0 (still in Beta) allows you to correct namespaces globally.
The other way to do it is to make the other developer fix the code.
It sounds like you are trying to replicate VB.Net's project namespace feature in C#. This is not a supported feature of the C# compiler or IDE. You will not be able to create one by modifying a project file. You will need to add the namespace to every file in the project either manually or via a tool.
I'm new to C#, and am curious about best practice for using namespaces.
I have a solution that contains a single class library project, along with several, small console app projects. All the console app projects do is parse command-line arguments and call different classes within the library project. The library project uses the standard CompanyName.Tool convention.
My question is, since the only purpose of a given console app is to a class in the library project, and will never itself be called from another project or class, do I need to put it inside a namespace? It seems unnecessary.
You are correct. It is unnecessary. The only reason you would want to be using a namespace is if you are creating libraries for re-use in many programs.
No, you dont need to. I find it's easier for maintainability to keep the entry points of an app (console, web, or windows) without namespaces.
The project will have a default namespace if you look in the properties. Any class not given an explicit namespace will inherit that one. So you don't need a namespace for any class unless it differs from the project one.