NOIDATUT E Leaning Platform Dot Net Framework Lab Manual COMPUTER SCIENCE AND ENGINEERING Presented by: NOIDATUT Email : e-learn@noidatut.com www.noidatut.com
C SHARP 1. Program to display Hello World in C Sharp 2. Program to illustrate Comments in C Sharp 3. Program to illustrate Data Types in C Sharp 4. Program to illustrate Ennumerations in C Sharp 5. Program to illustrate Arrays in C Sharp 6. Program to illustrate variables and constant 7. Program to illustrate Expressions in C Sharp 8. Program to illustrate loops and statements in C Sharp 9. Program to illustrate structures in C Sharp 10. Program to illustrate Class in C Sharp 11. Program to illustrate abstract Class in C Sharp 12. Program to implement inheritance in C Sharp 13. Program to illustrate exception handling in C Sharp 14. Program to create custom exception handling in C Sharp 15. Program to illustrate the use of delegates in C Sharp 16. Program to illustrate the use of events in C Sharp C SHARP LAB MANUAL 2 1. Program to display Hello World in C Sharp using System; public class HelloWorld
public static void Main() Console.WriteLine("Hello World in C#"); C# classes are stored in.cs files. After you have saved the preceding program in a file, let's say Hello World, you can compile the program into a.net executable. You will need to include the.net Framework SDK's bin folder in your PATH variable. If you have installed Visual Studio.NET, you have a shortcut under Visual Studio.NET Tools called Visual Studio.NET 2003 Command Prompt. This shortcut initializes all the environment variables and provides access to the command-line compilers as well. csc HelloWorld.cs Now enter HelloWorld.exe to run the application, and you should see "Hello World in C#" echoed on the screen WWW.NOIDATUT.COM C SHARP LAB MANUAL 3 2. Program to illustrate Comments in C Sharp using System; / <summary> / A Simple Class / </summary>
public class Comments public static void Main() / A Simple comment /* A multi line comment */ C#"); Console.WriteLine("Hello World in NOIDATUT.COM - E LEARNING PLATFORM C SHARP LAB MANUAL 4 3. Program to illustrate Data Types in C Sharp C# Type Corresponding.NET Framework Type Bool System.Boolean
byte, sbyte Char System.Byte, System.SByte System.Char decimal, double, System.Decimal, System.Double, Single short, ushort, System.Single System.Int16, System.UInt16, int, uint, long, System.Int32, System.UInt32, Ulong Object String System.Int64, System.UInt64 System.Object System.String C SHARP LAB MANUAL 5 4. Program to illustrate Ennumerations in C Sharp using System; public class UseEnumerations
enum CreditCard Visa = 0, MasterCard = 1, AmericanExpress = 2, Discover = 3 public static void Main() CreditCard mycard = CreditCard.Discover; Console.WriteLine(mycard); Console.WriteLine((int) mycard); C SHARP LAB MANUAL 6 5. Program to illustrate Arrays in C Sharp using System; public class UseArrays public static void Main()
String[] days_of_week = "Sunday", ; "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" foreach (string day in days_of_week) Console.WriteLine(day); C SHARP LAB MANUAL 7 6. Program to illustrate variables and constant Variables and Constants Simple value types can be assigned using the variable = value construct, whereas reference types are required to use the new keyword for creating a new instance. using System;
public class UseVariables public static void Main() const string HELLO_WORLD = "Hello World"; string message = HELLO_WORLD+ " in C#"; MyClass mc = new MyClass(message); mc.print(); public class MyClass private String message; public MyClass(String message) this.message = message; C SHARP LAB MANUAL 8 public void Print() Console.WriteLine(message);
C SHARP LAB MANUAL 9 7. Program to illustrate Expressions in C Sharp Expressions in C# are very similar to those provided by Java and C+ + programming languages. using System;
public class UseExpressions public static void Main() int a = 10; int b = 10; int result = a * b; bool check = (a == b); Console.WriteLine(result); Console.WriteLine(check); C SHARP LAB MANUAL 10 8. Program to illustrate loops and statements in C Sharp As expected, the C# programming language includes several procedural programming constructs, including if-else, for loop, while loop, switch statements, and so on.
using System; public class UseStatements public static void Main() string[] message = "Hello", "World", "in", "C#"; foreach (string msg in message) Console.Write(msg+" "); Console.WriteLine(""); int a = 10; int b = 20; if (a < b) C SHARP LAB MANUAL 11 Console.WriteLine("a<b"); else Console.WriteLine("a>=b");
C SHARP LAB MANUAL 12 9. Program to illustrate structures in C Sharp Structures are simply an aggregation of value types and are allocated on the stack and not on the heap. Structures are useful for passing a logical and related set of data values. Structures don't support inheritance but can implement interfaces. For instance, the following program would print Hitesh.Seth followed by John.Doe and illustrates that structures are not reference types.
using System; public class UseStructures public static void Main() Person hs = new Person("Hitesh","Seth"); Person jd = hs; jd.firstname = "John"; jd.lastname = "Doe"; Console.WriteLine(hs.FirstName+"."+hs.LastName); Console.WriteLine(jd.FirstName+"."+jd.LastName); C SHARP LAB MANUAL 13 public struct Person public string FirstName, LastName; public Person(string FirstName, string LastName)
this.lastname = LastName; this.firstname = FirstName; C SHARP LAB MANUAL 14 10. Program to illustrate Class in C Sharp Classes, on the other hand, are reference types and hence are allocated on the heap. Classes provide object-oriented constructs such as encapsulation, polymorphism, and inheritance. For instance, the following program would print John.Doe twice, illustrating that objects are reference types, allocated on the heap.
using System; public class UseClasses public static void Main() Person hs = new Person("Hitesh","Seth"); Person jd = hs; jd.firstname = "John"; jd.lastname = "Doe"; Console.WriteLine(hs.GetFullName()); Console.WriteLine(jd.GetFullName()); C SHARP LAB MANUAL 15 public class Person private string sfirstname, slastname; public Person(string FirstName, string LastName)
this.sfirstname = FirstName; this.slastname = LastName; public string FirstName get set return sfirstname; sfirstname = value; public string LastName get C SHARP LAB MANUAL 16 return slastname; set
slastname = value; public String GetFullName() return this.firstname + "."+ this.lastname; C SHARP LAB MANUAL 17 11. Program to illustrate Inheritance in Class in C Sharp Classes provide inheritance capability, which allows the derived class to inherit the functionality of a base class and potentially override some of the methods. A class definition consists of constructors and destructors, members, methods, properties, and events. (You will learn more about events later in this section.) Unlike the Java programming language, in
C# all methods that are overridden must be marked as virtual in the base class. The is operator provides runtime validation if an object is of a particular type. For instance, the following program will return that a FullPerson object is always a Person. using System; namespace hks public class UseInheritance public static void Main() FullPerson hs = new FullPerson("Hitesh","K","Seth"); Console.WriteLine(hs.GetFullName()); Object ohs = hs; if (ohs is Person) C SHARP LAB MANUAL 18 Console.WriteLine("I am still a Person"); public class Person
public string FirstName, LastName; public Person(string FirstName, string LastName) this.firstname = FirstName; this.lastname = LastName; public virtual string GetFullName() return this.firstname + "." + this.lastname; public class FullPerson : Person public string MiddleInitial; C SHARP LAB MANUAL 19 public FullPerson(string FirstName, string MiddleInitial, base(firstname,lastname) string LastName) : this.middleinitial = MiddleInitial;
public override string GetFullName() return this.firstname + "." + this.middleinitial + "." + this.lastname; C SHARP LAB MANUAL 20 12. Program to illustrate abstract Class in C Sharp Classes can also be marked as either abstract, which means they have to be subclassed for any instances to be created, or sealed, which does not allow any subclassing. Using Abstract Classes (C#)
using System; public class UseAbstractClasses public static void Main() Person hs = new Person("Hitesh","Seth"); Console.WriteLine(hs.GetFullName()); abstract public class Abstract protected string FirstName, LastName; public Abstract(string FirstName, string LastName) this.firstname = FirstName; this.lastname = LastName; abstract public string GetFullName(); public class Person : Abstract
public Person(string FirstName, string LastName) : base(firstname, LastName) public override string GetFullName() return FirstName+"."+LastName; C SHARP LAB MANUAL 22 13. Program to implement inheritance in C Sharp C# provides the concept of interfaces. Interfaces really represent a signature of what needs to be implemented by a derived class. C# supports multiple inheritances of interfaces using System;
public class UseInterfaces public static void Main() Person hs = new Person(); hs.name = "Hitesh Seth"; hs.address = "1 Executive Drive, City, NJ 08520"; Console.WriteLine(hs.GetName()); Console.WriteLine(hs.GetAddress()); public interface IName string GetName(); C SHARP LAB MANUAL 23 public interface IAddress string GetAddress(); public class Person : IName, IAddress private string name, address; public Person()
public string Name set name = value; public string Address set address = value; C SHARP LAB MANUAL 24 public string GetName() return name; public string GetAddress() return address;
C SHARP LAB MANUAL 25 14. Program to illustrate exception handling in C Sharp C# provides robust exception handling capabilities. For instance, the program that follows catches the exception at runtime and allows messages to be displayed to the end user without requiring an intermediate exit. using System;
public class UseExceptions public static void Main() try int a = 10; int b = 10; int c = a/(a-b); catch (Exception ex) Console.WriteLine("Exception Caught"); Console.WriteLine(ex.Message); C SHARP LAB MANUAL 26
C SHARP LAB MANUAL 2 7 15. Program to create custom exception handling in C Sharp Custom exceptions, which contain more information related to the underlying application, can also be created. Custom exceptions derive from the System.Exception class. Creating Custom Exceptions (C#) using System;
public class UseCustomExceptions public static void Main() try Discount big_discount = new Discount(56); catch (TooBigDiscountException ex) Console.WriteLine("Exception Caught"); Console.WriteLine(ex.Message); C SHARP LAB MANUAL 28 public class Discount private int percent; public Discount(int percent) this.percent = percent; if (percent > 50) throw new
TooBigDiscountException("Discount > 50%"); public class TooBigDiscountException : Exception public TooBigDiscountException(String msg) : base (msg) C SHARP LAB MANUAL 29 16. Program to illustrate the use of delegates in C Sharp Delegates give C# programmers the capability of function pointers, basically passing a function as a parameter. using System; public class UseDelegates public delegate void MyDelegate(string message);
public static void Main() String message = "Hello Delegates"; MyDelegate d1 = new MyDelegate(PrintOnce); MyDelegate d2 = new MyDelegate(PrintTwice); d1(message); d2(message); public static void PrintOnce(String message) Console.WriteLine(message); C SHARP LAB MANUAL 30 public static void PrintTwice(String message) Console.WriteLine("1."+message); Console.WriteLine("2."+message);
C SHARP LAB MANUAL 31 16. Program to illustrate the use of events in C Sharp A typical use of delegates is in event handling. For instance, take a look at Listing 3.5. It defines a class called Button, which has a Delegate called EventHandler. Event handlers can be assigned for the event OnClick and allow the calling application to pass in the reference of the method Button_Click as the callback method to invoke after the button is clicked. Using Events (C#) using System; public class Events public static void Main()
Button button = new Button(); button.onclick+= new Button.EventHandler(Button_Click) ; button.click(); public static void Button_Click() Console.WriteLine("Button Clicked"); C SHARP LAB MANUAL 31 public class Button public delegate void EventHandler(); public event EventHandler OnClick; public void Click() OnClick();