Two namespaces with the same object name - c#

I have two namespaces:
System.Numerics and UnityEngine
Both have the type Vector3.
So now when ever i want to use it i have to declare which namespace before it. Like this:
protected struct CVN
{
public Complex h;
public UnityEngine.Vector2 d;
public UnityEngine.Vector3 n;
}
Is there any way to define that i only want Vector3 from one namespace so i don't have to always type NameSpaceHere.Vector3 every single time ?
Or am i stuck with the tedious nature of stating the namespace every time. Especially since i only need the Complex type from Numerics its quite annoying.

If all you need from System.Numerics is Complex, then:
using UnityEngine;
using Complex = System.Numerics.Complex;
At the top of your file, without using System.Numerics; should do it.
This is an alias.

You can wrap the using directive of the wanted class in the namespace of your current class rather than putting it outside.
Consider this example
namespace System.Numerics
{
class MyClass
{
}
}
namespace UnityEngine
{
class MyClass
{
}
}
using System.Numeric;
namespace ConsoleApplication24
{
using UnityEngine; // inside the namespace
class Program
{
static void Main(string[] args)
{
MyClass xx = new MyClass(); // from UnitEngine instead of System.Numeric
}
}
}

Related

Master Import (using) file c#

So I have many classes for a unity video game and it is growing VERY quickly, however
I am using quite a few assets to help and as a result I have many using that
are repeated on each class.
Is there a way in c# or unity to have a master file that just contains all of them for example
namespace MasterImports {
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
}
then just adding something like this to each class by default.
using MasterImports;
I just think this makes managment of my skripts alot easier and
means I am unlikely to run into an error where I forget to add a using
then forgetting what one it was i used and having to hunt scripts for it.
This is a deliberate rule of C#. If you do this:
namespace Frobozz
{
namespace Magic
{
class Lamp {}
}
class Foo
{
Magic.Lamp myLamp; // Legal; Magic means Frobozz.Magic when inside Frobozz
}
}
That is legal. But this is not:
namespace Frobozz
{
namespace Magic
{
class Lamp {}
}
}
namespace Flathead
{
using Frobozz;
class Bar
{
Magic.Lamp myLamp; // Illegal; merely using Frobozz does not bring Magic into scope
}
}
Look into namespace aliases:
using n2 = n1.n2;
...
n2.foo something;
What is before the class name should be a complete name space (with/or other class name(s) for nested types). A truncated namespace will not work.

Manage namespace library (C#)

I am trying to organize a library. My issue is that it would quickly gets very large.
This is what i have so far:
namespace MyLibrary {
namespace Math {
namespace Geometry {
public class BezierCurve {
//...
};
}
namespace Combinatorics {
}
}
namespace Collections {
}
//...
}
Its not many lines of code now, but since BezierCurve alone is around 200 lines, creating all of the classes in the above document is not going to be the way to go.
Is it possible to somehow specify the content of BezierCurve in another document and include / refer to it in the namespace Geometry?
Multiple files can declare classes in the same namespace. There is no requirement for a namespace to be fully defined within a single file.
You should have a file per class.
e.g. your BezierCurve would typically be in:
MyLibrary/Math/Geometry/BezierCurve.cs
and be declared within
namespace MyLibrary.Math.Geometry
{
public class BezierCurve
{
}
}
It's recommended to have one type per file (well, with some exceptions). Thus you will have a single namespace without all this nested structure:
namespace MyLibrary.Math.Geometry
{
public class BezierCurve
{
// ...
}
}
If you want to refer BezierCurve in another file, just add a using directive with appropriate namespace:
using MyLibrary.Math.Geometry;
namespace MyLibrary.Math.Combinatorics
{
public class SomeClassFromCombinatoricsWhichUsesBezierCurve
{
// ...
}
}

Checking whether all my classes have the Serializable attribute

In my current software I make use of serialization and therefore need everything to be marked with the [Serializable] attribute.
Is there an easy way of checking this using my Visual Studio without going through them one at a time, or just waiting for it to crash?
To clarify, I don't need to know how to check if a class is serializable in code. I'm talking about using the IDE.
If you want to use reflection to find classes not marked [Serializable] you can use reflection to get the class types via GetTypes and then find only those not marked with Serializable.
try this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReflectOnSerializableAttr
{
class Program
{
static void Main(string[] args)
{
//use Linq
var q = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.IsClass && ((t.Attributes & TypeAttributes.Serializable) != TypeAttributes.Serializable)
select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));
Console.ReadKey();
}
}
[Serializable]
public class TestSerializableOne
{
public string SomeFunc() { return "somefunc"; }
}
public class TestForgotSerializable
{
private int _testInt = 200;
}
}
The above program outputs:
Program
TestForgotSerializable

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.

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

Categories

Resources