Day 5 - Coding for the Morning Session

0 views
Skip to first unread message

Nuwan

unread,
Jan 7, 2006, 4:45:45 AM1/7/06
to sliit-mcsd
Here are some of the programs that we discussed in the morning


// file : prg1.cs
public class test1 {

internal void method1() {

System.Console.WriteLine("hello");
}
}

// file : prg2.cs

public class test2 {

public void method2() {

System.Console.WriteLine("hello");
}

public static void Main() {

test1 myob = new test1();
myob.method1();
}
}


Note : We can compile both programs using the following command

csc prg1.cs prg2.cs /out:prgs

This will create an exe called prgs

-----------------------------------------

An assembly is distributable file which is either an exe or an dll.

We can compile prg1.cs as a dll using the following command

csc prg1.cs /target:libarary out:prg1.dll

This will create a dll file called prg1.dll

We can refer to this dll from the main program as follows from the
command line

csc prg2.cs /reference:prg1.dll

This will create an exe file called prg2.exe

If you look at ILDASM you will note that prg2.exe has a reference to
prg1.dll

In Visual Studio .NET you can have a reference to a dll by add one from
reference from the solution explorer.

------------------------------------------------
Note : Internal Access

If you change some of the public classes in the dll (prg1.cs) to
internal then you cannot access them from the prg2.cs main program.

internal access is public within the current assembly, but private
outside.

------------------------
Namespace Examples
// --------------------- prg1.cs

namespace Graphics {

public class test1 {

internal void method1() {

System.Console.WriteLine("hello");
}
}

}

// ---------------- prg3.cs

namespace SLIIT {

public class test1 {

public void method1() {

System.Console.WriteLine("hello");
}
}
}


public class test2 {
public void method2() {
System.Console.WriteLine("hello");
}

public static void Main() {
SLIIT.test1 mt = new SLIIT.test1();
mt.method1();
}
}


//------------------------

If you take the following example

namespace SLIIT.Graphics.Projects {

class XYZ {

}

}

You need to access XYZ as SLIIT.Graphics.Projects.XYZ;


It would be easy if have using the using clause

using SLIIT.Graphics.Projects;

Now you can directly access all the classes, structs defined under this
namespace.

Namespaces are used to avoid conflicts and are defined at a logical
level.

Reply all
Reply to author
Forward
0 new messages