This is my first time in C# programming.Below is my simple program on while loop.
May I know how to declare namespace for using int?
And where can I look for this namespace information.
Thanks
using
System;public
class WhileLoop(){
public static void Main(string[] args){
int i = 0; while ( i < 5 ){
Console.WriteLine ( i );++
i;}
}
}
}
AnttiPosted Dec 13, 2005, 3:35 PM
Hi, and welcome to C# world:)
Namespace defines a scope, where identifier, which refers to variable, class or namespace names, are visible to compiler (and other objects). For example, if you wan't to use class Int32 (which is same as int), you must use System namespace, while Int32 is defined inside the System namespace. If you don't add the
using System;to your code, the compiler wouldn't find the int at all, and cant compile your code.Basically namespace is just logical way to organize your code. You should always declare every class in its own file, and every namespace should have its own folder. This way you keep your code in good order. You don't have to do this, or declare your own namespaces at all, but it's very recommended, since they are very usefull feature indeed.
You can add your own class to your special namespace, so you can add identifiers with every name you want. You can refer to identifiers inside your namespace easily. For example if you have namespace called MyProgram.MySpcialNamespace, and there is a class called MyClass. You can make an object of MyClass with following code:
MyProgram.MySpcialNamespace.MyClass myObject = new MyProgram.MySpcialNamespace.MyClass();. Or you can add
using MyProgram.MySpcialNamespace;in the beginning of the code, and write
MyClass myObject = new MyClass();Identifier names can also be changed in code. This is called aliasing. If you want to locally rename Int32 class into MyInt, you can do it using using directive, as shown later in the example line 2.
Here is example program that shows some situations for using directive, and namespaces and scope:
using System; //add namespace to your code so you can use its services
using MyInt = System.Int32; //in C# this is called alias
namespace MyProgram.MySpcialNamespace //you can declare your own namespaces like this
{
    public class MyClass
    {
        static void Main(string[] args)
        {
            MyInt integerInFunctionScope = 999;
            //here you can create shorter scope for variable
            //than it normally would have, something like
            //a namespace, if you like.
            //Is this the aswer for your question?
            {
                int integerInItsOwnScope = 34;
                Console.WriteLine("integers: "+integerInItsOwnScope+" and "+integerInFunctionScope);
            }
            //the following line wouldn't compile!
            //Console.WriteLine(integerInItsOwnScope);
            Console.WriteLine("integers: "+integerInFunctionScope);
        }
    }
}
Hope this post helps you:) I recommend that you search for good C# book from you library. Books are really useful when learning new since they provide much greater perspective that single web entries. I personally recommend O'Reillys Programming C#. I my self started with that book, it starts from basics and goes far enough for everyday usage.