C# Namespaces used as class equivalent - c#

Does anyone come across with some older C# code as below, what would the equivalent in order to over come the later compiler error "'car' is a 'namespace' but is used like a 'type'".
Many thanks in advance.
namespace something.car
{
public class Display : UserControl
{
private car _car; // comppiler error here
public car carConfig // comppiler error here
{
get
{
return this._car;
}
set
{
this._car = value;
}
}
}
}

Change your first line from namespace something.car to
namespace Something.SomethingElse
and make sure you have a class car defined somewhere which you use in lines private car _car; and public car carConfig.
As a side comment, it is quite common to spell namespaces and class names in Pascal notation, e. g. namespace Something and public class Car.

To be able to use it without changing the namespace name ("but for sure change the namespace name is the right way").
but you can use the file path for the class when you use it, so if the car class is namespace "SomenameSpace.Viechels"
then you could use like below to not have the above conflict:
SomenameSpace.Viechels.car _car;
SomenameSpace.Viechels.car _carConfig;

Related

Creating a separate file for main, interface and class

I'm starting to learn how to create C# and using Interface and Class.
Can someone teach me how to separate my Class and my Interface and still maintain their connection from one another?
Reason: If I'll be updating my code, I'll know where to add them and it will be less code in my screen.
namespace Car
{
class MainClass
{
//My Interface
interface ICar
{
int gas { get; set; }
//void refuel();
int getGasLeft();
}
//My Class
class Car : ICar
{
public int gas { get; set; }
public Car(string _name)
{
name = _name;
dist = 0;
gas = 40;
}
public void refuel(int lit)
{
gas += lit;
}
public int getGasLeft()
{
return gas;
}
}
//Main
public static void Main(string[] args)
{
Car toyota = new Car();
toyota.drive(100);
toyota.refuel(5);
Console.WriteLine("Name: " + toyota.name +
"\nGas Left: " + toyota.getGasLeft());
Console.ReadLine();
}
}
}
In C#, a namespace is the way to connect files from different places.
For example, you can make a folder Interfaces and put your ICar there. I will show you an example from my project.
My folder structure looks like this:
In my ViewModel.cs I use an object named FakeData. As you can see below, class FakeData is in the TaskManager.Models namespace.
Now, we are going to our project and we are looking at the Models folder.
In C#, when you create a folder and a class in it, you will get a namespace based on your folder name. Of course, you can change it. You can edit the namespace how you want to.
In your case, you can simply make a folder called Interfaces. Right click, then select New Class, and give it a name, for example, Icar.cs. Paste your code there. Your default namespace will be ProjectName.Interfaces. Make your interface public. Then, you can call your interface by adding using ProjectName.Interfaces in the top of your main class. Then you can do Main : ICar without any errors.
Same idea with the Car class. You can make a folder named Models and a class in it that called Cars.cs. It will have the namespace ProjectName.Models. If you want to use your interface that is in Interfaces folder, you need to add a namespace to your Car.cs class. In your case, it will look like using ProjectName.Interfaces. Then, you can call the interface in Car.cs without any error.
Feel free to ask me any questions about this. I will try to help you.
You need to learn about public, private, protected, namespaces, and so on.
On your case, create a new file called ICar.cs in the same folder and declare it as public interface in the same namespace.

Namespace name and type name conflict

Is it possible to make the following code valid in C# without changing my namespace or type name. I'm wondering if there is a trick or keyword around this problem?
namespace NS
{
public class Foo { }
}
namespace NS.Foo
{
public class Bar { }
}
The error I'm getting is "The namespace 'NS' already contains a definition for 'Foo'".
No - Imagine having a subclass in Foo called Bar:
namespace NS
{
public class Foo { public class Bar {} }
}
namespace NS.Foo
{
public class Bar { }
}
How can you ever tell the difference if one does: new NS.Foo.Bar()?
Essentially, no there isn't. Even if you could somehow resolve the immediate ambiguity, this problem goes all the way down: what happens if you declare a property of Foo named Bar?
Rename one of your Foos.
No - there's no way to disambiguate from NS.Foo the namespace and NS.Foo the class.

Is it possible to do relative C# namespace reference?

I have namespaces:
MyProject.Core.Db
MyProject.Core.Model
And I have classes:
MyProject.Core.Db.User
MyProject.Core.Model.User
Is it possible something like:
using MyProject.Core;
namespace MyProject.BLL
{
public class Logic
{
public static void DoSomething()
{
var userEntity = new Db.User();
var userModel = new Model.User();
}
}
}
I just want to avoid using suffixes in class names (UserModel, UserEntity).
Is it possible to do in somehow in C#?
I don't understand why people say it's not possible. Surely it is possible, you just need to be a bit more specific in the namespaces when you create the target classes (ie you can omit only the common part of the namespace):
namespace MyProject.Core.Db
{
public class User
{
}
}
namespace MyProject.Core.Model
{
public class User
{
}
}
namespace MyProject.BLL
{
public class Logic
{
public static void DoSomething()
{
var foo = new Core.Db.User();
var boo = new Core.Model.User();
}
}
}
The way you're avoiding a fully qualified name within BLL is by being inside of a common namespace with the other two.
What you're trying to achieve is not possible. The closest thing you will get is a using alias directive which looks like this:
using User = Myproject.Core.Db.User;
This will remove the need to fully qualify the path for Myproject.Core.Db.User. You will still need to specify the fully qualified path for at least one of the classes, though. You could create another alias for the other type as Servy demonstrated but at this point I would just rename the classes.
I think the real solution here is to give your classes more descriptive identifiers.
C# does support relative namespace references.
In your case, that means if you're in the namespace MyProject.Core, you can references your classes as Db.User and Model.User. But if you're in the namespace MyProject.BLL, you have to include the Core prefix (Core.Db.User and Core.Model.User).
If that's not good enough for your and you don't want to change your namespace structure, your best choice is probably to add usings to all files that use the types in question.
using DbUser = MyProject.Core.Db.User;
using ModelUser = MyProject.Core.Model.User;
One thing you can do, and we probably should do a lot more, is to specify usings relative to the current namespace. To do this, just move your usings inside the namespace declaration. It doesn't fix your stated problem, but the shorter relative paths are less brittle and your project will be easier to refactor.
namespace MyProject.Core{
using Db;
using Model;
You can add an alias for the one class that you don't import the namespace of:
using MyProject.Core.Db;
using ModelUser = MyProject.Core.Model.User;
namespace MyProject.BLL
{
public class Logic
{
public static void DoSomething()
{
var userEntity = new User();
var userModel = new ModelUser();
}
}
}
In C# it's not possible to use the example that's shown; it's simply not a supported feature.

Namespace vs Class Declaration

I'm new to C# and I can't seem to find any info on this so I will ask it here.
Do classes in namespaces have to ever be declared?
using System;
public class myprogram
{
void main()
{
// The console class does not have to be declared?
Console.WriteLine("Hello World");
}
}
If I'm not using a namespace then I have to declare a class
class mathstuff
{
private int numberone = 2;
private int numbertwo = 3;
public int addhere()
{
return numberone + numbertwo;
}
using System;
public class myprogram
{
void main()
{
// the class here needs to be declared.
mathstuff mymath = new mathstuff();
Console.WriteLine(mymath.addhere());
}
}
Am I understanding this correctly?
A namespace is simply a way to make clear in which context the class is living in. Think of your own name, Ralph. We have many Ralphs in this world, but one of that is you. An extra way to get rid of the ambiguity is to add your surname. So that if we have 2 Ralphs, we have a bigger chance of talking about you.
The same works for classes. If you define class AClass and you would have the need of define another class AClass there would be no way to distinguish between the two. A namespace would be that 'surname'. A way of having to classes, but still able to distinguish between the two different classes, with the same name.
To answer your question, it has nothing to do with "not having to declare". It would only be easier to write code.
For example:
using System;
public class myprogram
{
void main()
{
// the class here needs to be declared.
Console.WriteLine("blah");
}
}
Because of the using System; you don't have to declare the namespace of Console. There is only one Console available, which lives in the System namespace. If you wouldn't declare your using System; namespace then you'd need to explain where Console can be found. Like this.
System.Console.WriteLine("blah");
From MSDN:
The namespace keyword is used to declare a scope. This namespace scope lets you organize code and gives you a way to create globally unique types.
For more info check MSDN for namespace.
I think what you mean is "can you declare a class without a namespace?". Yes you can, it's referred to as the global namespace.
class BaseClass
{
}
class SubClass : global::BaseClass
{
}
However, this is very bad practice, and you should never do this in a production application.

Namespace-only class visibility in C#/.NET?

In C#, can you make a class visible only within its own namespace without living in a different assembly? This seems useful for typical helper classes that shouldn't be used elsewhere.
(i.e. what Java calls package-private classes)
You can make the classes internal but this only prevents anyone outside of the assembly from using the class. But you still have to make a separate assembly for each namespace that you want to do this with. I'm assuming that is why you wouldn't want to do it.
Getting the C# Compiler to Enforce Namespace Visibility
There is an article here (Namespace visibility in C#) that shows a method of using partial classes as a form of "fake namespace" that you might find helpful.
The author points out that this doesn't work perfectly and he discusses the shortcomings. The main problem is that C# designers designed C# not to work this way. This deviates heavily from expected coding practices in C#/.NET, which is one of the .NET Frameworks greatest advantages.
It's a neat trickā€¦ now don't do it.
I don't think that what you want is possible.
internal is assembly (strictly speaking module) privacy. It has no effect on namespace visibility.
The only way to achieve privacy of a class from other classes within the same assembly is for a class to be an inner class.
At this point if the class is private it is invisible to anything not in that class or the outer class itself.
If protected it is visible to everyone that could see it when private but is also visible to sub classes of the outer class.
public class Outer
{
private class Hidden { public Hidden() {} }
protected class Shady { public Shady() {} }
public class Promiscuous { public Promiscuous() {} }
}
public class Sub : Outer
{
public Sub():base()
{
var h = new Hidden(); // illegal, will not compile
var s = new Shady(); // legal
var p = new Promiscuous(); // legal
}
}
public class Outsider
{
public Outsider()
{
var h = new Outer.Hidden(); // illegal, will not compile
var s = new Outer.Shady() // illegal, will not compile
var p = new Outer.Promiscuous(); // legal
}
}
In essence the only way to achieve what you desire is to use the outer class as a form of namespace and restrict within that class.
No, it is possible. You can use internal class in another assembly.
For example I have a internal string extension class that located in SharMillSoft.Core assembly, if I want use it in another assembly that name is SharpMilSoft.Extension, I must use assembly attribute like as below:
using System;
using System.Linq;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("SharpMilSoft.Extensions")]
namespace SharpMilSoft.Core.Extensions.Strings.Public
{
internal static class SharpStringExtensions
{
public static bool IsNullOrEmpty(this string data)
{
return string.IsNullOrEmpty(data);
}
}
}
And I use this class in SharpMilSoft.Extension assembly like as below:
namespace SharpMilSoft.Extensions.Strings
{
public static class SharpStringExtensions
{
public static bool IsNullOrEmpty(this string data)
{
return Core.Extensions.Strings.Public.SharpStringExtensions.IsNullOrEmpty(data);
}
}
}
Note: Then SharpMilSoft.Extensions assembly will be friend assembly for SharpMilSoft.Core assembly
For more details about friend assembly, you can visit this link : Friend assemblies
If you have a single assembly you can define as many namespaces in that assembly as you want but no matter what modifier you apply in the IDE you will always be able to see the classes in other namespaces.
Not sure if it is directly possible, but a few good ways to fake it would be:
1) Have the classes that need this sort of stuff inherit from a single class which has the helper class as an internal class.
2) Use extension methods and then only reference the extension methods within the namespace.

Categories

Resources