/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
using System;
// Demonstrate a namespace.
// Declare a namespace for counters.
namespace Counter {
// A simple countdown counter.
class CountDown {
int val;
public CountDown(int n) { val = n; }
public void reset(int n) {
val = n;
}
public int count() {
if(val > 0) return val--;
else return 0;
}
}
}
public class NSDemo1 {
public static void Main() {
Counter.CountDown cd1 = new Counter.CountDown(10);
int i;
do {
i = cd1.count();
Console.Write(i + " ");
} while(i > 0);
Console.WriteLine();
Counter.CountDown cd2 = new Counter.CountDown(20);
do {
i = cd2.count();
Console.Write(i + " ");
} while(i > 0);
Console.WriteLine();
cd2.reset(4);
do {
i = cd2.count();
Console.Write(i + " ");
} while(i > 0);
Console.WriteLine();
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Demonstrates using as a statement
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Using.cs — Demonstrates using as a statement
//
// Compile this program with the following command line:
// C:>csc Using.cs
using System;
using System.Windows.Forms;
using System.Drawing;
using Pen = System.Drawing.Pen;
using PaintHandler = System.Windows.Forms.PaintEventHandler;
namespace nsForm
{
public class UsingForm : Form
{
public UsingForm ()
{
this.Text = “Using Statement”;
this.Paint += new PaintHandler(this.OnPaint);
}
static public void Main ()
{
Application.Run(new UsingForm());
}
private Color [] clr = new Color []
{
Color.Red,
Color.Green,
Color.Blue
};
private void OnPaint (object obj, PaintEventArgs e)
{
Rectangle client = this.ClientRectangle;
int side = (client.Right – client.Left) / 3;
for (int x = 0; x < 3; ++x)
{
using (Pen pen = new Pen(clr[x], (float) 2.0))
{
client = Rectangle.Inflate (client, -10, -10);
e.Graphics.DrawEllipse (pen, client);
}
}
}
}
}
[/csharp]
Using namespace
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
namespace nsFirst
{
public class NameSpaceDemo
{
static public void Main ()
{
nsSecond.clsClass cls = new nsSecond.clsClass ();
for (nsNested.WeekDays x = nsNested.WeekDays.Sunday; x < nsNested.WeekDays.DaysInWeek; ++x)
cls.ShowDay (x);
}
}
namespace nsNested
{
public enum WeekDays
{
Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, DaysInWeek
};
}
}
namespace nsSecond
{
class clsClass
{
public void ShowDay (nsFirst.nsNested.WeekDays day)
{
Console.WriteLine (day);
}
}
}
[/csharp]
How the using statement is used to specify namespaces
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example6_10.cs illustrates how the using
statement is used to specify namespaces
*/
using Sybex;
using System;
public class Example6_10
{
public static void Main()
{
// the Console.WriteLine() call uses the System namespace
Console.WriteLine("Creating a Sybex.Car object");
// create a Car object (uses the Sybex namespace)
Car myCar = new Car();
myCar.make = "Toyota";
Console.WriteLine("myCar.make = " + myCar.make);
}
}
namespace Sybex
{
// declare the Car class
public class Car
{
public string make;
}
}
The use of namespace hierarchies (part 1)
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example6_8.cs illustrates the use of namespace
hierarchies (part 1)
*/
public class Example6_8
{
public static void Main()
{
Sybex.UserInterface.MyClass myUI = new Sybex.UserInterface.MyClass();
Sybex.DatabaseAccess.MyClass myDB = new Sybex.DatabaseAccess.MyClass();
// uses class in MiddleTier namespace in Example6_9
Sybex.MiddleTier.MyClass myMT = new Sybex.MiddleTier.MyClass();
// call the Test() methods
myUI.Test();
myDB.Test();
myMT.Test();
}
}
namespace Sybex
{
namespace UserInterface // nested namespace
{
public class MyClass
{
public void Test()
{
System.Console.WriteLine("UserInterface Test()");
}
}
}
}
namespace Sybex.DatabaseAccess // nested namespace using dot
{
public class MyClass
{
public void Test()
{
System.Console.WriteLine("DatabaseAccess Test()");
}
}
}
/*
Example6_9.cs illustrates the use of namespace
hierarchies (part 2)
*/
namespace Sybex // use the Sybex namespace
{
namespace MiddleTier // another namespace
{
public class MyClass {
public void Test() {
System.Console.WriteLine("MiddleTier Test()");
}
}
}
}
Illustrates the use of two namespaces
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example6_7.cs illustrates the use of two namespaces
*/
public class Example6_7
{
public static void Main()
{
// create a Sybex.Car object
System.Console.WriteLine("Creating a Sybex.Car object");
Sybex.Car myCar = new Sybex.Car();
myCar.make = "Toyota";
System.Console.WriteLine("myCar.make = " + myCar.make);
// create a DifferentCompany.Car object
System.Console.WriteLine("Creating a DifferentCompany.Car object");
DifferentCompany.Car myOtherCar = new DifferentCompany.Car();
myOtherCar.make = "Porsche";
System.Console.WriteLine("myOtherCar.make = " + myOtherCar.make);
}
}
// create the Sybex namespace
namespace Sybex
{
// declare the Car class
public class Car
{
public string make;
}
}
// create the DifferentCompany namespace
namespace DifferentCompany
{
// declare the Car class
public class Car
{
public string make;
}
}
Namespaces can be nested
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Namespaces can be nested.
using System;
namespace NS1 {
class ClassA {
public ClassA() {
Console.WriteLine("constructing ClassA");
}
}
namespace NS2 { // a nested namespace
class ClassB {
public ClassB() {
Console.WriteLine("constructing ClassB");
}
}
}
}
public class NestedNSDemo {
public static void Main() {
NS1.ClassA a= new NS1.ClassA();
// NS2.ClassB b = new NS2.ClassB(); // Error!!! NS2 is not in view
NS1.NS2.ClassB b = new NS1.NS2.ClassB(); // this is right
}
}