Post #8 Namespaces
Hi Everyone. Let's talk about the first interesting feature.
Namespaces: Now you may remember me mentioning these before. Namespaces are an interesting feature in C#. They allow you to have separate functions with the same name. Essentially they allow you to isolate and create separate versions of your code. Lets look at an old but good example. It's our favorite program. Hello World!
using System;
namespace Namespaces
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
This is probably the easiest program to understand namespaces. Now you may look at this and think that this is just a program that writes hello world. That's not an entirely accurate. This is a program that has a namespace that writes "Hello World!". In this case this namespace is called Namespaces that has the main function(method) that runs the program. That being said let's add some other namespaces.
using System;
namespace math25V1
{
class Math
{
public static void Math25(){
int x = 5;
x = x * x;
Console.WriteLine(x);
}
}
}
namespace math25V2
{
class Math
{
public static void Math25(){
int x = 5;
int y = 20;
x = x + y;
Console.WriteLine(x);
}
public static void Math25V2(){
int x = 5;
x = x + x + x + x + x;
Console.WriteLine(x);
}
}
}
namespace Namespaces
{
class Program
{
static void Main(string[] args)
{
math25V1.Math.Math25();
math25V2.Math.Math25();
math25V2.Math.Math25V2();
Console.WriteLine("Hello World!");
}
}
}
This is probably the more complex way of doing this. There's 2 namespaces with unique names but the same class. By same class I mean that if you look very closely at the lines added to the hello world program or the namespace Namespaces as that namespace is called. You will notice that it works similarly to referencing different classes. You reference the namespace you want access to. Then you reference the class. Then you reference the function. Two namespaces reference the class Math but that is actually two takes on the same class. You couldn't have two classes with the same name in java. Constructors were a way to create objects in multiple ways but you couldn't have a class that has two entirely different things. In this case it's to ways to math to 25. It could be one way to math to 25 and a second class that finds half of a number. It could be one class called arrays reads and prints arrays while a class called arrays combines two arrays. The code in a namespace is isolated and organized apart from any other namespace.
Global Namespace: There's also a global namespace where a variable declared outside of all namespaces can be used in all namespaces.
The same namespace can actually be used in multiple files. It can be defined in the collective files. Namespaces actually help so that you don't interfere with the other developers you work with. If you and others are working on a project. Lets say your doing part A and your friends are doing part B and C. Normally anything you write can effect what they are doing. Namespaces allow you to only effect what you did rather than what they have already done. This would also be useful for as I did above writing multiple projects that do the same thing. System is a namespace to allow access to the C# and .Net Frameworks. You can write like you do in Java System.something to access something. You can also use the "using" key word to add a namespace to the current namespaces being used. If two classes have the same name as they do above you need to manually help the system to know what you are using. You can also use a namespace to create a file. Before you can make use of a type you need to use a reference command line. Just an example from one of my books Microsoft Visual C# .Net.
csc /reference:system.xml.dll; animals.cs
I believe this references one of the many types in the .net framework and will create the file animals.cs.
Every type and namespace has a fully qualified name which uniquely identifies the namespace and type among all of the others. Paraphrasing from The C# Programming Language Fourth Edition Anders Hejlsberg and the other authors.
A compilation unit runs all the code in a file and defines the overall structure. A compilation unit can have zero or more global variables followed by zero or more namespaces. This was just a side note but kind of important to understanding namespaces.
I didn't say this but hopefully you figured it out by now namespaces are declared using the namespace keyword. They are also are public. They are assumed to be public and cannot have access modifiers. You can also have nested namespaces.
namespace N1.N2
{
class A { }
class B { }
}
is also equal to
namespace N1
{
namespace N2
{
class A { }
class B { }
}
}
That's an example from The C# Programming Language Fourth Edition Anders Hejlsberg and the other authors.
I think I was wrong somewhere namespaces can have the same name but can't have 2 classes with the same name if they do. 2 namespaces with the same name are considered 2 references to one namespace. All the references are added together to one universal namespace when you access it.
Extern Alias: An Extern Alias is apparently another identifier for a namespace. Actually it's kind of a way of fixing the problem I mentioned above. It can be used to create multiple instances of the same namespace. It also creates a way to have 2 classes with the same name. If you have 2 aliases now they won't interact with each other but rather they will do whatever you defined them to do. You must have an Extern Alias definition.
extern alias X;
extern alias Y;
class Test
{
X::N.A a;
X::N.B b1;
Y::N.B b2;
Y::N.C c;
}
That's an example from The C# Programming Language Fourth Edition Anders Hejlsberg and the other authors. Essentially creates two aliases then defines them. Notice the two classes N.B have the same name but will get different identifiers. This is very interesting.
Using Directives: This is basically just using the using keyword to access namespaces and extern aliases.
Namespace Members: Anything using the namespace declared for them.
I know this was long. Leave comments below.
These are interesting times. If you find yourselves needing to talk to someone feel free to reach out in email. We will continue on and continue into the future together.
This is the end of this post.
I used all three books below.
Resources: Same Resources
MICROSOFT VISUAL C# .NET by Mickey Williams
PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde
PROGRAMMING BASICS FOR ABSOLUTE BEGINNERS by NATHAN CLARK
The C# Programming Language Fourth Edition by Anders Hejlsberg, Mads Torgusen,
Scott Wiltamuth and Peter Golde
Visual Studio Code Environment:
Link to get started:
Comments
Post a Comment