Normally, students have an implicit trust in their teachers. For they know that if the blind lead the blind, both shall fall into the ditch. However, our philosophy is that a good teacher has to be an even better liar!!
In accordance with this belief, initially we told you that WriteLine is the name of a function. Then we told you that the name is not WriteLine, it is System.Console.WriteLine. But even that's not true.
Now comes the moment of truth.
Console is actually the name of a class. Yes, trust us! There is no crying wolf here. The class Console has a function called WriteLine. Hence the name now becomes Console.WriteLine. However, that leaves out the word System. Now what does System mean?
Well, a number of functions like WriteLine are available in C#. Some functions will let you print, some will enable writing of data to disk and others will let you create graphics. The problem that we are posed with is - how does one remember which function satisfies what purpose?
Wouldn't it make sense if we logically grouped similar functions together? So, Microsoft thought that all functions that can write to the screen could be made part of one class. All functions that let you work with pictures could be part of another class. But even then, you will have too many functions in one class. So they thought of having a single higher logical group. Such that all the functions that have anything to do with the screen, i.e. whether you are drawing pictures or writing text, be grouped once again into a higher body. Thus all classes that deal with interacting with a database could go into a group called Data.
The second problem that we are posed with is that of name clashes. What do we mean by that? Now, nothing can stop me from creating my own class called Console and include a function called WriteLine in it. But how will C# know which Console am I referring to? The one that we created ourselves, or the one that Microsoft has already created. In order to resolve these problems Microsoft decided to take classes and put them into namespaces. What is a namespace? It is simply a word. Thus we can logically group everything as per namespaces.
From the above explanation you would have by now guessed that System is nothing but a namespace. The following programs will help make this concept crystal clear.
a.cs
class zzz
{
static void Main()
{
yyy.abc();
abc();
zzz.abc();
}
public static void abc()
{
System.Console.WriteLine("abc in zzz ");
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
Compiler Error
a.cs(5,1): error CS0246: The type or namespace name 'yyy' could not be found (are you missing a using directive or an assembly reference?)
In the above program, the only change that we have made is that we have now included the class yyy in a namespace called vijay. On doing so you will realize that the same program that worked earlier doesn't work anymore. This is because yyy is put in a namespace vijay.
A namespace is nothing but a word with an open and close brace. The entire class is enclosed within the namespace. If you want to access a function belonging to class yyy from another class then the only way to do so is by saying vijay.yyy.abc(); Thus you specify the namespace first, then the name of the class followed by the name of the function, each of them separated by dots. Thus like Urdu, we read anything from the right and not the left. We start with the name of a function, then the name of the class and whatever is left is the namespace.
Here zzz has not been given a namespace. If you don't specify a namespace then by default the class is included in a global namespace. Now change yyy.abc(); to vijay.yyy.abc() and watch the error disappear.
a.cs
class zzz
{
static void Main()
{
vijay.yyy.abc();
abc();
zzz.abc();
}
public static void abc()
{
System.Console.WriteLine("abc in zzz ");
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
Output
abc
abc in zzz
abc in zzz
We bet your faces are now beaming! Seems like the secret of happiness is not in doing what one likes to do but in liking what one has to do. We had to get rid of the error which we have succeeded in doing. The error has disappeared; the program executes as advertised and it generates the same output as it did previously.
Thus all the classes and functions created by Vijay can be included in a namespace called vijay. If Sonal creates a namespace by her name then she can include all the functions and classes created by her in the namespace sonal. Thus there will be no name clashes at all. These namespaces that are created by us are called user-defined namespaces whereas System is a pre-defined namespace. Thus System is a namespace in which a class called Console was created, which contained a function called WriteLine.
Thus the namespace concept allows us to create logical groups. So all xml related classes can be in a namespace called xml, web can be in a web namespace and so on and so forth.
But the only problem now is that when you start writing code you have to specify the namespace first, then the class name followed by the function name. Well, everything is available but for a price! Our consolation is that it is a very small price to pay.
a.cs
namespace mukhi
{
class zzz
{
static void Main()
{
vijay.yyy.abc();
abc();
zzz.abc();
mukhi.zzz.abc();
}
public static void abc()
{
System.Console.WriteLine("abc in zzz ");
}
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
Output
abc
abc in zzz
abc in zzz
abc in zzz
In the above program, we have two classes zzz and yyy. The class zzz is included in a namespace called mukhi and the class yyy is included in a namespace called vijay.
So when we say abc(); in Main, Main is in zzz, so you are actually writing mukhi.zzz.abc(); This is because C# will automatically expand it since the function abc is available within the same class. Hence it is at your discretion as to how you want to write it. You can say abc(), zzz.abc() or mukhi.zzz.abc(); finally they all expand to namespace.classname.function name. C# adds the name of the namespace and the name of the class even if you do not specifically write it. The concept of namespaces is not very difficult to understand. It allows for a hierarchical grouping of classes. It tells us which classes are logically grouped. It also avoids classes from having the same name.
a.cs
class zzz
{
static void Main()
{
Console.WriteLine("abc in zzz ");
}
}
Compiler Error
a.cs(5,1): error CS0246: The type or namespace name 'Console' could not be found (are you missing a using directive or an assembly reference?)
If we do not enclose our class in a namespace, it becomes part and parcel of the global namespace ‘ ‘. This namespace does not contain a class called Console. We had mentioned earlier that the class Console is contained in the namespace System. We do not want to preface the Console class with the namespace System each and every time. The only reason being that our fingers will wear out typing the word System over and over again. So C# lets us use a shorthand by means of which we avoid the pain of having to keep on writing the name of a namespace over and over again.
a.cs
using System;
class zzz
{
static void Main()
{
Console.WriteLine("abc in zzz ");
}
}
Output
abc in zzz
The secret here is not in doing great things, but doing a small thing in a great way. We get no error simply by employing the word using which is part of the C# language. All that using does is whenever it sees only the name of a class, it goes and adds (in this case) the word System. Thus we do not have to write the word System over and over again. This works the way shorthand does.
a.cs
using System;
class zzz
{
static void Main()
{
yyy.abc();
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
Console.WriteLine("abc");
}
}
}
Compiler Error
a.cs(6,1): error CS0246: The type or namespace name 'yyy' could not be found (are you missing a using directive or an assembly reference?)
Now we get an error for yyy and not for Console as the yyy class belongs to the vijay namespace and not the global namespace. Because of the using keyword, C# adds the namespace System to yyy yielding System.yyy.abc and realizes that System does not contain a class called yyy. Hence the error.
a.cs
using System;
using vijay;
class zzz {
static void Main()
{
yyy.abc();
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
Console.WriteLine("abc");
}
}
}
Output
abc
The error vanishes as C# first tries System.yyy.abc gets an error, then tries vijay.yyy.abc and is successful. Thus by having two using's we do not have to write the namespaces vijay or System ever again.
a.cs
using System;
using mukhi;
using vijay;
namespace mukhi
{
class zzz
{
static void Main()
{
yyy.abc();
abc();
zzz.abc();
zzz.abc();
}
public static void abc()
{
System.Console.WriteLine("abc in zzz ");
}
}
}
namespace vijay
{
class yyy
{
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
Output
abc
abc in zzz
abc in zzz
abc in zzz
We can have as many using's as we like and the compiler will try each one in turn. If none of them match we will receive an error. In this case it will try 3 times with System, mukhi and vijay and if none match, you will get an error.
a.cs
using System.Console;
class zzz
{
static void Main()
{
WriteLine("abc in zzz ");
}
}
Compiler Error
a.cs(1,7): error CS0138: A using namespace directive can only be applied to namespaces; 'System.Console' is a class not a namespace
After the word using you can only write the name of a namespace. System.Console is a namespace class combination which is not allowed.
Building Hierarchy
In C# you organize classes using namespaces. Now let's discover the extent we can go to as far as organizing classes.
a.cs
class zzz
{
static void Main()
{
mukhi.vijay.yyy.abc();
}
}
namespace mukhi
{
namespace vijay
{
class yyy {
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
}
Output
abc
In this program we have a namespace within a namespace i.e. within the namespace mukhi we have another namespace vijay. Thus namespaces are 'hierarchical'. If you want to access the function abc in yyy you have to specify it in the form- namespace.classname.functionname. So, the qualified name is now mukhi.vijay.yyy.abc(); Once the function is called, WriteLine will display 'abc'.
In order to differentiate between the various names separated by dots, always read backwards. Reading backwards, the first is the function name then the class name and the names thereafter will all be namespaces.
Alternatively, you can directly specify the namespace as mukhi.vijay, as we have done below. This program generates the same output as previously, it prints abc.
a.cs
class zzz
{
static void Main()
{
mukhi.vijay.yyy.abc();
}
}
namespace mukhi.vijay
{
class yyy
{
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
Output
abc
Here we have a single namespace by the name mukhi.vijay. The name mukhi.vijay is actually a shortcut for defining a namespace named mukhi that contains a namespace named vijay. In this program, we have only two namespaces. But you can expand it further to include a number of namespaces depending upon the level of hierarchy required by your program.
We can liken this to an organization. Let's consider mukhi to be the name of the company. Within that you have a sub-company or a division called vijay, which creates it own classes. As such the level of hierarchy can be expanded.
Before you understand the next program let's address a simple question. Why do you use classes? Classes are used because they offer a large number of functions. You don't use classes because of the variables that you can create within them; you use classes for the functions that they provide. Remember, you call a function using the form -namespace.classname.functionname.
File Operations
a.cs
class zzz
{
static void Main()
{
File.Copy("c:\\csharp\\a.txt","c:\\csharp\\b.txt",true);
}
}
Compiler Error
a.cs(5,1): error CS0246: The type or namespace name 'File' could not be found (are you missing a using directive or an assembly reference?)
Our next program will enlighten you on something most sought after - The art of Copying. Ah! Finally something of interest!
This program introduces the 'Copy' function. It allows you to duplicate a file. File is a class and it has a function called Copy, which is static.
The first parameter 'a.txt' is the source file i.e. the file, which we want to duplicate. The second parameter 'b.txt' is the destination file i.e. the file that we want to copy to. Note that you must specify the entire path for the file name. The last parameter 'true' implies that if the file exists then it will be overwritten. If the file does not exist it will be created and contents of the source file will be copied onto it.
And just when you thought you had mastered the art of copying the program returns with an error message. The error says C# does not know what File.Copy is. The problem is that the name of the namespace is System.IO. So you have to specify the namespace too.
Add the namespace and execute the program.
a.cs
class zzz
{
static void Main()
{
System.IO.File.Copy("c:\\csharp\\a.txt","c:\\csharp\\b.txt",true);
}
}
The program does not generate any compilation errors. Create a file called a.txt with some text before you run this program. Execute this program and then open the file 'b.txt'. Finally, the task has been accomplished! You now have the contents of a.txt copied into b.txt.
Our next program introduces another function called 'Delete'.
a.cs
class zzz
{
static void Main()
{
System.IO.File.Delete("c:\\csharp\\a.txt");
}
}
The above program takes the name of a file as the parameter. This function will remove the file specified from disk. Give the dir command at the command prompt and you will find that the file has been deleted.
Every language will offer you millions of such functions like copy and delete. These functions were always available, but C# has gone one step further and made these functions a part of a Class. They are now part of a Namespace. Hence it becomes easier to categorize functions. It is but a question of detail whether you should or should not categorize them.
No comments:
Post a Comment