/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* This is a simple C# program. Call this program Example.cs. */ using System; public class ExampleWriteLine { // A C# program begins with a call to Main(). public static void Main() { Console.WriteLine("A simple C# program."); } }
Language Basics
Variations on the Main() Method
/* The previous iteration of Main() was defined to take a single parameter (an array of strings) and return an integer data type. This is not the only possible form of Main(), however. It is permissible to construct your application¡¯s entry point using any of the following signatures (assuming it is contained within a C# class or structure definition): // No return type, array of strings as argument. public static void Main(string[] args) { } // No return type, no arguments. public static void Main() { } // Integer return type, no arguments. public static int Main() { } */
choose between two overloaded methods at run-time using the 'is' keyword
using System; public class BankAccount { virtual public void Withdraw() { Console.WriteLine("Call to BankAccount.Withdraw()"); } } public class SavingsAccount : BankAccount { override public void Withdraw() { Console.WriteLine("Call to SavingsAccount.Withdraw()"); } } public class MainClass { public static void Main(string[] strings) { BankAccount ba = new BankAccount(); Test(ba); SavingsAccount sa = new SavingsAccount(); Test(sa); } public static void Test(BankAccount baArgument) { if (baArgument is SavingsAccount) { SavingsAccount saArgument = (SavingsAccount)baArgument; saArgument.Withdraw(); } else { baArgument.Withdraw(); } } }
The is operator confirms that the employee is a manager.
using System; public class Starter { public static void Main() { Manager person = new Manager("Accounting"); Console.WriteLine("[Menu] "); Console.WriteLine("Task 1"); Console.WriteLine("Task 2"); if (person is IManager) { IManager mgr = person; Console.WriteLine(" [{0} Menu] ",mgr.Department); Console.WriteLine("Task 3"); } } } public interface IManager { string Department { get; } } public class Employee { } public class SalariedEmployee : Employee { } public class Manager : SalariedEmployee, IManager { public Manager(string dept) { propDepartment = dept; } private string propDepartment; public string Department { get { return propDepartment; } } }
is Checker
using System; using System.Collections.Generic; using System.Text; class Checker { public void Check(object param1) { if (param1 is ClassA) Console.WriteLine("Variable can be converted to ClassA."); else Console.WriteLine("Variable can't be converted to ClassA."); if (param1 is IMyInterface) Console.WriteLine("Variable can be converted to IMyInterface."); else Console.WriteLine("Variable can't be converted to IMyInterface."); if (param1 is MyStruct) Console.WriteLine("Variable can be converted to MyStruct."); else Console.WriteLine("Variable can't be converted to MyStruct."); } } interface IMyInterface { } class ClassA : IMyInterface { } class ClassB : IMyInterface { } class ClassC { } class ClassD : ClassA { } struct MyStruct : IMyInterface { } class Program { static void Main(string[] args) { Checker check = new Checker(); ClassA try1 = new ClassA(); ClassB try2 = new ClassB(); ClassC try3 = new ClassC(); ClassD try4 = new ClassD(); MyStruct try5 = new MyStruct(); object try6 = try5; Console.WriteLine("Analyzing ClassA type variable:"); check.Check(try1); Console.WriteLine(" Analyzing ClassB type variable:"); check.Check(try2); Console.WriteLine(" Analyzing ClassC type variable:"); check.Check(try3); Console.WriteLine(" Analyzing ClassD type variable:"); check.Check(try4); Console.WriteLine(" Analyzing MyStruct type variable:"); check.Check(try5); Console.WriteLine(" Analyzing boxed MyStruct type variable:"); check.Check(try6); Console.ReadKey(); } }
Using the is Keyword to Work with an Interface
using System; public interface IPrintMessage { void Print(); }; class Class1 { public void Print() { Console.WriteLine("Hello from Class1!"); } } class Class2 : IPrintMessage { public void Print() { Console.WriteLine("Hello from Class2!"); } } class MainClass { public static void Main() { PrintClass PrintObject = new PrintClass(); PrintObject.PrintMessages(); } } class PrintClass { public void PrintMessages() { Class1 Object1 = new Class1(); Class2 Object2 = new Class2(); PrintMessageFromObject(Object1); PrintMessageFromObject(Object2); } private void PrintMessageFromObject(object obj) { if(obj is IPrintMessage) { IPrintMessage PrintMessage; PrintMessage = (IPrintMessage)obj; PrintMessage.Print(); } } }
Use internal
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Use internal. using System; class InternalTest { internal int x; } public class InternalDemo { public static void Main() { InternalTest ob = new InternalTest(); ob.x = 10; // can access -- in same file Console.WriteLine("Here is ob.x: " + ob.x); } }