Skip to main content

Posts

Showing posts with the label C#

Encapsulation example in c#

1. BankAccountExternal.cs using System; public class BankAccountExternal { public decimal GetAmount() { return 2000.00m; } } 2. BankAccountPrivate.cs using System; class BankAccountPrivate { private string m_name; public string CustomerName { get { return m_name; } set { m_name = value ; } } } 3. BankAccountProtected.cs using System; class BankAccountProtected { public void CloseAccount() { ApplyPenalties(); CalculateFinalInterest(); DeleteAccountFromDB(); } protected virtual void ApplyPenalties() { // deduct from account } protected virtual void CalculateFinalInterest() { // add to account } protected virtual void DeleteAccountFromDB() { // send notification to data entry personnel } } 4. BankAccountPublic.cs using System; class BankAccountPublic { public decimal GetAmount() { return 1000.00...

Introduction to Enums

using System; // declares the enum public enum Volume : byte { Low = 1, Medium, High } class EnumBaseAndMembers { static void Main() { // create and initialize // instance of enum type Volume myVolume = Volume.Low; // make decision based // on enum value switch (myVolume) { case Volume.Low: Console.WriteLine(" The volume has been turned Down. "); break ; case Volume.Medium: Console.WriteLine(" The volume is in the middle. "); break ; case Volume.High: Console.WriteLine(" The volume has been turned up. "); break ; } Console.ReadLine(); } }

Introduction to Exception Handling

using System; using System.IO; class ExceptionDemo { static void Main( string [] args) { FileStream outStream = null ; FileStream inStream = null ; try { outStream = File.OpenWrite(" DestinationFile.txt "); inStream = File.OpenRead(" BogusInputFile.txt "); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { if (outStream != null ) { outStream.Close(); Console.WriteLine(" outStream closed. "); } if (inStream != null ) { inStream.Close(); Console.WriteLine(" inStream closed. "); } } } }

Introduction to Delegates

using System; // this is the delegate declaration public delegate int Comparer( object obj1, object obj2); public class Name { public string FirstName = null ; public string LastName = null ; public Name( string first, string last) { FirstName = first; LastName = last; } // this is the delegate method handler public static int CompareFirstNames( object name1, object name2) { string n1 = ((Name)name1).FirstName; string n2 = ((Name)name2).FirstName; if (String.Compare(n1, n2) > 0) { return 1; } else if (String.Compare(n1, n2) < 0) { return -1; } else { return 0; } } public override string ToString() { return FirstName + " " + LastName; } } class SimpleDelegate { Name[] names = new Name[5]; public SimpleDelegate() { names[0] ...

Introduction to Events

using System; using System.Drawing; using System.Windows.Forms; // custom delegate public delegate void StartDelegate(); class EventDemo : Form { // custom event public event StartDelegate StartEvent; public EventDemo() { Button clickMe = new Button(); clickMe.Parent = this ; clickMe.Text = " Click Me "; clickMe.Location = new Point((ClientSize.Width - clickMe.Width) /2,(ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "StartDelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new StartDelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked( object se...

Interface example in c#-1

using System; interface IParentInterface { void ParentInterfaceMethod(); } interface IMyInterface : IParentInterface { void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine(" MethodToImplement() called. "); } public void ParentInterfaceMethod() { Console.WriteLine(" ParentInterfaceMethod() called. "); } }

Interface example in c#

  interface IMyInterface { void MethodToImplement(); }   using System; class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine(" MethodToImplement() called. "); } }

Structs in C#

  using System; struct Point { public int x; public int y; public Point( int x, int y) { this .x = x; this .y = y; } public Point Add(Point pt) { Point newPt; newPt.x = x + pt.x; newPt.y = y + pt.y; return newPt; } } /// <summary> /// Example of declaring and using a struct /// </summary> class StructExample { static void Main( string [] args) { Point pt1 = new Point(1, 1); Point pt2 = new Point(2, 2); Point pt3; pt3 = pt1.Add(pt2); Console.WriteLine(" pt3: {0}:{1} ",pt3.x,pt3.y); } }

How to create Abstract class in C#.

Abstract class class :- 1)Concreet class(We can create object of that) **method:- concreet method(method having body) 2)Abstract class(we can not create object, because it is too general ) **method :-1) abstract method(method without body) 2) concrete method it is used to inherit Code: abstract class human { //Abstract methods public abstract void hair(); //Concrete method public void display() { System.Console.WriteLine("hello"); } } class boys : human { public override void hair() { System.Console.WriteLine("Small"); } } class girls : human { public override void hair() { System.Console.WriteLine("LONG"); } } class a { public static void Main() { boys b=new boys(); girls g=new girls(); b.hair(); g.hair(); b.display(); g.display(); } }