Wednesday, February 12, 2014
RethrowingExceptions in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RethrowingExceptions
{
class Program
{
static void DoSomeMath()
{
int x = 10, y = 0;
int result;
try
{
result = x / y;
Console.WriteLine("Result is {0}", result);
}
catch
{
Console.WriteLine("Error in DoSomeMath()");
throw new ArithmeticException();
}
}
static void Main(string[] args)
{
try
{
DoSomeMath();
}
catch (ArithmeticException e)
{
Console.WriteLine("Hmm, there was an error in there, be careful!");
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RethrowingExceptions
{
class Program
{
static void DoSomeMath()
{
int x = 10, y = 0;
int result;
try
{
result = x / y;
Console.WriteLine("Result is {0}", result);
}
catch
{
Console.WriteLine("Error in DoSomeMath()");
throw new ArithmeticException();
}
}
static void Main(string[] args)
{
try
{
DoSomeMath();
}
catch (ArithmeticException e)
{
Console.WriteLine("Hmm, there was an error in there, be careful!");
}
Console.ReadLine();
}
}
}
Exception Objects In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionObject
{
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 0;
int result;
try
{
result = x / y;
Console.WriteLine("The result is: {0}", result);
}
catch (System.DivideByZeroException e)
{
Console.WriteLine("Whoops! You tried to divide by zero!");
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Just proving that this code always runs.");
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExceptionObject
{
class Program
{
static void Main(string[] args)
{
int x = 10;
int y = 0;
int result;
try
{
result = x / y;
Console.WriteLine("The result is: {0}", result);
}
catch (System.DivideByZeroException e)
{
Console.WriteLine("Whoops! You tried to divide by zero!");
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Just proving that this code always runs.");
}
Console.ReadLine();
}
}
}
Custome Exceptions iN C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomExceptions
{
public class NoJoesException : Exception
{
public NoJoesException() : base("We don't allow no Joes in here!")
{
this.HelpLink = "http://www.joemarini.com/";
}
}
class Program
{
static string GetName()
{
string s = Console.ReadLine();
if (s.Equals("Joe"))
throw new NoJoesException();
return s;
}
static void Main(string[] args)
{
string theName;
try
{
theName = GetName();
Console.WriteLine("Hello {0}", theName);
}
catch (NoJoesException nje)
{
Console.WriteLine(nje.Message);
Console.WriteLine("For help, visit: {0}", nje.HelpLink);
}
finally
{
Console.WriteLine("Have a nice day!");
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomExceptions
{
public class NoJoesException : Exception
{
public NoJoesException() : base("We don't allow no Joes in here!")
{
this.HelpLink = "http://www.joemarini.com/";
}
}
class Program
{
static string GetName()
{
string s = Console.ReadLine();
if (s.Equals("Joe"))
throw new NoJoesException();
return s;
}
static void Main(string[] args)
{
string theName;
try
{
theName = GetName();
Console.WriteLine("Hello {0}", theName);
}
catch (NoJoesException nje)
{
Console.WriteLine(nje.Message);
Console.WriteLine("For help, visit: {0}", nje.HelpLink);
}
finally
{
Console.WriteLine("Have a nice day!");
}
Console.ReadLine();
}
}
}
Using Struct inC#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UsingStructs
{
public struct Point
{
private int xCoord;
private int yCoord;
public Point(int x, int y)
{
xCoord = x;
yCoord = y;
}
public int x
{
get { return xCoord; }
set { xCoord = value; }
}
public int y
{
get { return xCoord; }
set { xCoord = value; }
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UsingStructs
{
public struct Point
{
private int xCoord;
private int yCoord;
public Point(int x, int y)
{
xCoord = x;
yCoord = y;
}
public int x
{
get { return xCoord; }
set { xCoord = value; }
}
public int y
{
get { return xCoord; }
set { xCoord = value; }
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
Using Interfaces In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UsingInterfaces
{
interface ITalkative
{
void SayHello();
void SayGoodBye();
}
class myExampleClass : ITalkative
{
public myExampleClass()
{
}
public void SayHello()
{
Console.WriteLine("Saying hello!");
}
public void SayGoodBye()
{
Console.WriteLine("Saying goodbye!");
}
}
class Program
{
static void Main(string[] args)
{
myExampleClass myEC = new myExampleClass();
myEC.SayHello();
myEC.SayGoodBye();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UsingInterfaces
{
interface ITalkative
{
void SayHello();
void SayGoodBye();
}
class myExampleClass : ITalkative
{
public myExampleClass()
{
}
public void SayHello()
{
Console.WriteLine("Saying hello!");
}
public void SayGoodBye()
{
Console.WriteLine("Saying goodbye!");
}
}
class Program
{
static void Main(string[] args)
{
myExampleClass myEC = new myExampleClass();
myEC.SayHello();
myEC.SayGoodBye();
Console.ReadLine();
}
}
}
Sealed Classes Representation In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SealedClasses
{
class myExampleClass
{
public static string myMethod(int arg1)
{
return String.Format("You sent me the number {0}", arg1);
}
}
class mySubClass : myExampleClass
{
}
class Program
{
static void Main(string[] args)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SealedClasses
{
class myExampleClass
{
public static string myMethod(int arg1)
{
return String.Format("You sent me the number {0}", arg1);
}
}
class mySubClass : myExampleClass
{
}
class Program
{
static void Main(string[] args)
{
}
}
}
Method Overriding In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverriding
{
class baseClass
{
public virtual void doSomething()
{
Console.WriteLine("This is the baseClass saying hi!");
}
}
class subClass : baseClass
{
public override void doSomething()
{
base.doSomething();
Console.WriteLine("This is the subClass saying hi!");
}
}
class Program
{
static void Main(string[] args)
{
baseClass obj1 = new subClass();
obj1.doSomething();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverriding
{
class baseClass
{
public virtual void doSomething()
{
Console.WriteLine("This is the baseClass saying hi!");
}
}
class subClass : baseClass
{
public override void doSomething()
{
base.doSomething();
Console.WriteLine("This is the subClass saying hi!");
}
}
class Program
{
static void Main(string[] args)
{
baseClass obj1 = new subClass();
obj1.doSomething();
Console.ReadLine();
}
}
}
Method Overloading In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverloading
{
class Wine
{
public int Year;
public string Name;
public decimal price;
public Wine(string s)
{
Name = s;
}
public Wine(string s, int y)
{
Name = s;
Year = y;
}
public Wine(string s, decimal p, int y)
{
Name = s;
price = p;
Year = y;
}
}
class Program
{
static void Main(string[] args)
{
Wine w1 = new Wine("Charles Shaw Merlot");
Wine w2 = new Wine("Mark Ryan Dissident", 2004);
Wine w3 = new Wine("Dom Perignon", 120.00m, 1994);
Console.WriteLine("{0}", w1.Name);
Console.WriteLine("{0} {1}", w2.Year, w2.Name);
Console.WriteLine("{0} {1} {2}", w3.Year, w3.Name, w3.price);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverloading
{
class Wine
{
public int Year;
public string Name;
public decimal price;
public Wine(string s)
{
Name = s;
}
public Wine(string s, int y)
{
Name = s;
Year = y;
}
public Wine(string s, decimal p, int y)
{
Name = s;
price = p;
Year = y;
}
}
class Program
{
static void Main(string[] args)
{
Wine w1 = new Wine("Charles Shaw Merlot");
Wine w2 = new Wine("Mark Ryan Dissident", 2004);
Wine w3 = new Wine("Dom Perignon", 120.00m, 1994);
Console.WriteLine("{0}", w1.Name);
Console.WriteLine("{0} {1}", w2.Year, w2.Name);
Console.WriteLine("{0} {1} {2}", w3.Year, w3.Name, w3.price);
Console.ReadLine();
}
}
}
Abstract classes in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractClasses
{
abstract class myBaseClass
{
public abstract int myMethod(int arg1, int arg2);
}
class myDerivedClass : myBaseClass
{
public override int myMethod(int arg1, int arg2)
{
return arg1 + arg2;
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractClasses
{
abstract class myBaseClass
{
public abstract int myMethod(int arg1, int arg2);
}
class myDerivedClass : myBaseClass
{
public override int myMethod(int arg1, int arg2)
{
return arg1 + arg2;
}
}
class Program
{
static void Main(string[] args)
{
}
}
}
Stack In C#(How it works)
using System;
using System.Collections; // we need to add this reference to use Files and Directory information
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stacks
{
class Program
{
static void Main(string[] args)
{
Stack myStack = new Stack();
myStack.Push("item 1");
myStack.Push("item 2");
myStack.Push("item 3");
Console.WriteLine("{0} Items on the stack", myStack.Count);
// Have a peek at the top item
Console.WriteLine("{0}", myStack.Peek());
myStack.Pop(); // pops "item 3" off the top
// now "item 2" is the top item
Console.WriteLine("{0}", myStack.Peek());
myStack.Clear(); // get rid of everything
Console.WriteLine("{0} Items on the stack", myStack.Count);
Console.ReadLine();
}
}
}
using System.Collections; // we need to add this reference to use Files and Directory information
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stacks
{
class Program
{
static void Main(string[] args)
{
Stack myStack = new Stack();
myStack.Push("item 1");
myStack.Push("item 2");
myStack.Push("item 3");
Console.WriteLine("{0} Items on the stack", myStack.Count);
// Have a peek at the top item
Console.WriteLine("{0}", myStack.Peek());
myStack.Pop(); // pops "item 3" off the top
// now "item 2" is the top item
Console.WriteLine("{0}", myStack.Peek());
myStack.Clear(); // get rid of everything
Console.WriteLine("{0} Items on the stack", myStack.Count);
Console.ReadLine();
}
}
}
Tuesday, February 4, 2014
Ques in C#
using System;
using System.Collections; // we need to add this reference to use Files and Directory information
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Queues
{
class Program
{
static void Main(string[] args)
{
Queue myQ = new Queue();
myQ.Enqueue("item 1");
myQ.Enqueue("item 2");
myQ.Enqueue("item 3");
myQ.Enqueue("item 4");
Console.WriteLine("There are {0} items in the Queue", myQ.Count);
while (myQ.Count > 0)
{
string str = (string)myQ.Dequeue();
Console.WriteLine("Dequeueing object {0}", str);
}
Console.ReadLine();
}
}
}
using System.Collections; // we need to add this reference to use Files and Directory information
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Queues
{
class Program
{
static void Main(string[] args)
{
Queue myQ = new Queue();
myQ.Enqueue("item 1");
myQ.Enqueue("item 2");
myQ.Enqueue("item 3");
myQ.Enqueue("item 4");
Console.WriteLine("There are {0} items in the Queue", myQ.Count);
while (myQ.Count > 0)
{
string str = (string)myQ.Dequeue();
Console.WriteLine("Dequeueing object {0}", str);
}
Console.ReadLine();
}
}
}
Array List In C#
using System;
using System.Collections; // we need to add this reference to use Files and Directory information
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayLists
{
class Program
{
static void Main(string[] args)
{
ArrayList myAL = new ArrayList();
myAL.Add("one");
myAL.Add(2);
myAL.Add("three");
myAL.Add(4);
myAL.Insert(0, 1.25f);
foreach (object o in myAL)
{
if (o is int)
Console.WriteLine("{0}", o);
}
Console.ReadLine();
}
}
}
using System.Collections; // we need to add this reference to use Files and Directory information
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayLists
{
class Program
{
static void Main(string[] args)
{
ArrayList myAL = new ArrayList();
myAL.Add("one");
myAL.Add(2);
myAL.Add("three");
myAL.Add(4);
myAL.Insert(0, 1.25f);
foreach (object o in myAL)
{
if (o is int)
Console.WriteLine("{0}", o);
}
Console.ReadLine();
}
}
}
Value And Reference Types in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ValAndRefTypes
{
public class Point
{
public int x;
public int y;
}
class Program
{
static void testFunc1(int arg1)
{
arg1 += 10;
Console.WriteLine("arg1 is {0}", arg1);
}
static void testFunc2(Point pt)
{
Console.WriteLine("pt.x is {0}", pt.x);
pt.x += 10;
Console.WriteLine("pt.x is {0}", pt.x);
}
static void Main(string[] args)
{
var i = 10;
testFunc1(i);
Console.WriteLine("i is {0}", i);
Console.WriteLine();
Point p = new Point();
p.x = 10;
p.y = 10;
Console.WriteLine("p.x is {0}", p.x);
testFunc2(p);
Console.WriteLine("p.x is {0}", p.x);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ValAndRefTypes
{
public class Point
{
public int x;
public int y;
}
class Program
{
static void testFunc1(int arg1)
{
arg1 += 10;
Console.WriteLine("arg1 is {0}", arg1);
}
static void testFunc2(Point pt)
{
Console.WriteLine("pt.x is {0}", pt.x);
pt.x += 10;
Console.WriteLine("pt.x is {0}", pt.x);
}
static void Main(string[] args)
{
var i = 10;
testFunc1(i);
Console.WriteLine("i is {0}", i);
Console.WriteLine();
Point p = new Point();
p.x = 10;
p.y = 10;
Console.WriteLine("p.x is {0}", p.x);
testFunc2(p);
Console.WriteLine("p.x is {0}", p.x);
Console.ReadLine();
}
}
}
Defining properties in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningProperties
{
class Wine
{
private string Name;
private int year;
private string Apellation;
private decimal wholesalePrice;
private const decimal retailMarkup = 1.35m;
public decimal Price
{
get
{
return wholesalePrice * retailMarkup;
}
set
{
wholesalePrice = value;
}
}
public string MenuDescription
{
// only a getter for this property, which is generated from private fields
get { return year.ToString() + " " + Name + ", " + Apellation; }
}
public Wine(int y, string sName, string sApp, decimal wp)
{
Name = sName;
year = y;
Apellation = sApp;
wholesalePrice = wp;
}
}
class Program
{
static void Main(string[] args)
{
// Create some new Wine objects
Wine w1 = new Wine(2003, "Chateau Ste. Michelle Merlot", "Seven Hills", 23.50m);
Wine w2 = new Wine(2005, "Mark Ryan Dissident", "Ciel du Cheval", 40.00m);
// Write out the property values
// Note that in these cases we are using the property getters
Console.WriteLine("Wine 1: {0}, {1}", w1.MenuDescription, w1.Price);
Console.WriteLine("Wine 2: {0}, {1}", w2.MenuDescription, w2.Price);
Console.WriteLine();
// change the wholesale price of one of the wines using the setter
w2.Price = 45.0m;
// write out the wine description, note how the retail price automatically changes
Console.WriteLine("Wine 2: {0}, {1}", w2.MenuDescription, w2.Price);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningProperties
{
class Wine
{
private string Name;
private int year;
private string Apellation;
private decimal wholesalePrice;
private const decimal retailMarkup = 1.35m;
public decimal Price
{
get
{
return wholesalePrice * retailMarkup;
}
set
{
wholesalePrice = value;
}
}
public string MenuDescription
{
// only a getter for this property, which is generated from private fields
get { return year.ToString() + " " + Name + ", " + Apellation; }
}
public Wine(int y, string sName, string sApp, decimal wp)
{
Name = sName;
year = y;
Apellation = sApp;
wholesalePrice = wp;
}
}
class Program
{
static void Main(string[] args)
{
// Create some new Wine objects
Wine w1 = new Wine(2003, "Chateau Ste. Michelle Merlot", "Seven Hills", 23.50m);
Wine w2 = new Wine(2005, "Mark Ryan Dissident", "Ciel du Cheval", 40.00m);
// Write out the property values
// Note that in these cases we are using the property getters
Console.WriteLine("Wine 1: {0}, {1}", w1.MenuDescription, w1.Price);
Console.WriteLine("Wine 2: {0}, {1}", w2.MenuDescription, w2.Price);
Console.WriteLine();
// change the wholesale price of one of the wines using the setter
w2.Price = 45.0m;
// write out the wine description, note how the retail price automatically changes
Console.WriteLine("Wine 2: {0}, {1}", w2.MenuDescription, w2.Price);
Console.ReadLine();
}
}
}
Defining Class Cont.......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningAClass
{
class MyClass
{
int myInteger;
string myMessage;
void myFunction()
{
Console.WriteLine("In my function");
}
public MyClass() {
myInteger = 0;
myMessage = "";
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningAClass
{
class MyClass
{
int myInteger;
string myMessage;
void myFunction()
{
Console.WriteLine("In my function");
}
public MyClass() {
myInteger = 0;
myMessage = "";
}
}
}
Defining Class In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningAClass
{
class MyClass
{
int myInteger;
string myMessage;
public static int myStaticInt = 100;
public int myFunction()
{
return myInteger;
}
public MyClass()
{
myInteger = 50;
myMessage = "This is a string";
}
}
class Program
{
static void Main(string[] args)
{
MyClass myC = new MyClass();
Console.WriteLine("Calling myFunction: {0}", myC.myFunction());
Console.WriteLine("Using a static member: {0}", MyClass.myStaticInt);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DefiningAClass
{
class MyClass
{
int myInteger;
string myMessage;
public static int myStaticInt = 100;
public int myFunction()
{
return myInteger;
}
public MyClass()
{
myInteger = 50;
myMessage = "This is a string";
}
}
class Program
{
static void Main(string[] args)
{
MyClass myC = new MyClass();
Console.WriteLine("Calling myFunction: {0}", myC.myFunction());
Console.WriteLine("Using a static member: {0}", MyClass.myStaticInt);
Console.ReadLine();
}
}
}
Variable scope
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VariableScope
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
int var1 = 20;
Console.WriteLine("The value of var1 at pass {0} is {1} ", i, var1);
}
//Console.WriteLine("{0}", var1);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace VariableScope
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
int var1 = 20;
Console.WriteLine("The value of var1 at pass {0} is {1} ", i, var1);
}
//Console.WriteLine("{0}", var1);
Console.ReadLine();
}
}
}
Loops In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Loops
{
class Program
{
static void Main(string[] args)
{
int myVal = 15;
// basic while loop
Console.WriteLine("Basic while() loop:");
while (myVal < 20) {
Console.WriteLine("myVal is currently {0}", myVal);
// careful! Always make sure that the loop has some way to exit
// or you'll end up with an Infinite Loop!
myVal += 3;
}
Console.WriteLine();
// the do-while loop
Console.WriteLine("The do-while() loop:");
do
{
Console.WriteLine("myVal is currently {0}", myVal);
myVal += 3;
} while (myVal < 20);
Console.WriteLine();
// the for loop
Console.WriteLine("The for loop:");
for (int i = 0; i < myVal; i += 5)
{
Console.WriteLine("i is currently {0}", i);
}
Console.WriteLine();
// using the continue and break keywords
Console.WriteLine("Using break and continue :");
for (int i = 0; i < 10; i++)
{
if (i == 5)
continue; // skip the rest of the loop
if (i == 9)
break;
Console.WriteLine("i is currently {0}", i);
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Loops
{
class Program
{
static void Main(string[] args)
{
int myVal = 15;
// basic while loop
Console.WriteLine("Basic while() loop:");
while (myVal < 20) {
Console.WriteLine("myVal is currently {0}", myVal);
// careful! Always make sure that the loop has some way to exit
// or you'll end up with an Infinite Loop!
myVal += 3;
}
Console.WriteLine();
// the do-while loop
Console.WriteLine("The do-while() loop:");
do
{
Console.WriteLine("myVal is currently {0}", myVal);
myVal += 3;
} while (myVal < 20);
Console.WriteLine();
// the for loop
Console.WriteLine("The for loop:");
for (int i = 0; i < myVal; i += 5)
{
Console.WriteLine("i is currently {0}", i);
}
Console.WriteLine();
// using the continue and break keywords
Console.WriteLine("Using break and continue :");
for (int i = 0; i < 10; i++)
{
if (i == 5)
continue; // skip the rest of the loop
if (i == 9)
break;
Console.WriteLine("i is currently {0}", i);
}
Console.ReadLine();
}
}
}
Function And Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FuncsAndMethods
{
class Program
{
static void Main(string[] args)
{
int result1;
result1 = formula(14);
Console.WriteLine("The result is: {0}", result1);
Console.ReadLine();
}
static int formula(int theVal)
{
return (theVal * 2) / 3 + 15;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FuncsAndMethods
{
class Program
{
static void Main(string[] args)
{
int result1;
result1 = formula(14);
Console.WriteLine("The result is: {0}", result1);
Console.ReadLine();
}
static int formula(int theVal)
{
return (theVal * 2) / 3 + 15;
}
}
}
TypeCasting in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TypeCasting
{
class Program
{
static void Main(string[] args)
{
Program objProg = new Program();
objProg.MyMethod("I am Inayat");
Console.ReadKey();
}
void MyMethod(object obj)
{
if (obj is int)
{
Console.WriteLine("this is a int");
}
else if (obj is string)
{
Console.WriteLine("this is a string");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TypeCasting
{
class Program
{
static void Main(string[] args)
{
Program objProg = new Program();
objProg.MyMethod("I am Inayat");
Console.ReadKey();
}
void MyMethod(object obj)
{
if (obj is int)
{
Console.WriteLine("this is a int");
}
else if (obj is string)
{
Console.WriteLine("this is a string");
}
}
}
}
Usin loops and printin Shape
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchStatmnt
{
class Program
{
static void Main(string[] args)
{
for (int i= 1;i <= 10; i++)
{
for (int j = 1; j<=i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
for (int i = 1; i <= 10; i++)
{
for (int j = i; j <= 10; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SwitchStatmnt
{
class Program
{
static void Main(string[] args)
{
for (int i= 1;i <= 10; i++)
{
for (int j = 1; j<=i; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
for (int i = 1; i <= 10; i++)
{
for (int j = i; j <= 10; j++)
{
Console.Write("*");
}
Console.WriteLine("");
}
Console.ReadKey();
}
}
}
Rectangleapplication: it returns the lenght and width
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rectangleapplication
{
class Rectangle
{
double length;
double width;
public void AcceptDetails()
{
length = 4.6;
width = 3.8;
}
public double GetArea()
{
return width * length;
}
public void DisPlay()
{
Console.WriteLine("length: {0}", length);
Console.WriteLine("Width : {0}", width);
Console.WriteLine("Area:{0}", GetArea());
}
}
class ExcuteTactangle
{
static void Main(string[] args)
{
Rectangle p = new Rectangle();
p.AcceptDetails();
p.DisPlay();
p.GetArea();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rectangleapplication
{
class Rectangle
{
double length;
double width;
public void AcceptDetails()
{
length = 4.6;
width = 3.8;
}
public double GetArea()
{
return width * length;
}
public void DisPlay()
{
Console.WriteLine("length: {0}", length);
Console.WriteLine("Width : {0}", width);
Console.WriteLine("Area:{0}", GetArea());
}
}
class ExcuteTactangle
{
static void Main(string[] args)
{
Rectangle p = new Rectangle();
p.AcceptDetails();
p.DisPlay();
p.GetArea();
Console.ReadLine();
}
}
}
Is Operator in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IsOperatorSample
{
public class Animal
{
}
public class Vehicle
{
}
public class Car : Vehicle
{
}
class Program
{
static void Main(string[] args)
{
Vehicle v = new Vehicle();
Animal a = new Animal();
Car c = new Car();
if (c is Vehicle)
{
Vehicle newV = (Vehicle)c;
Console.WriteLine(c.ToString() + " is a Vehicle. ");
}
else
{
Console.WriteLine(c.ToString() + " is NOT a Vehicle. ");
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IsOperatorSample
{
public class Animal
{
}
public class Vehicle
{
}
public class Car : Vehicle
{
}
class Program
{
static void Main(string[] args)
{
Vehicle v = new Vehicle();
Animal a = new Animal();
Car c = new Car();
if (c is Vehicle)
{
Vehicle newV = (Vehicle)c;
Console.WriteLine(c.ToString() + " is a Vehicle. ");
}
else
{
Console.WriteLine(c.ToString() + " is NOT a Vehicle. ");
}
Console.ReadLine();
}
}
}
ExplicitConversion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExplicitConversion
{
class Program
{
static void Main(string[] args)
{
double d = 656.67;
int i;
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ExplicitConversion
{
class Program
{
static void Main(string[] args)
{
double d = 656.67;
int i;
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
}
}
DoubleVsDecimal in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoubleVsDecimal
{
class Program
{
static void Main(string[] args)
{
float fnum1 = 2.6f;
float fnum2 = 4.0f;
Console.WriteLine("{0}" , fnum2/fnum1);
double istnum = 2.6d;
double secndnum = 4.0d;
Console.WriteLine("{0}" , secndnum / istnum);
decimal num1 = 2.6m;
decimal num2 = 2.9m;
Console.WriteLine("{0}" , num2 / num1 );
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DoubleVsDecimal
{
class Program
{
static void Main(string[] args)
{
float fnum1 = 2.6f;
float fnum2 = 4.0f;
Console.WriteLine("{0}" , fnum2/fnum1);
double istnum = 2.6d;
double secndnum = 4.0d;
Console.WriteLine("{0}" , secndnum / istnum);
decimal num1 = 2.6m;
decimal num2 = 2.9m;
Console.WriteLine("{0}" , num2 / num1 );
Console.ReadKey();
}
}
}
Const And Enums in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstAndEnum
{
public enum weekdays
{
sunday,
monday
}
// demonstrates how to use the enum
class Program
{
static void Main()
{
// create and initialize
// instance of enum type
//Volume myVolume = Volume.High;
weekdays wK = weekdays.monday;
// make decision based
// on enum value
switch (wK)
{
case weekdays.sunday:
Console.WriteLine("the day isa sunday.");
break;
case weekdays.monday:
Console.WriteLine("The day is monday.");
break;
}
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConstAndEnum
{
public enum weekdays
{
sunday,
monday
}
// demonstrates how to use the enum
class Program
{
static void Main()
{
// create and initialize
// instance of enum type
//Volume myVolume = Volume.High;
weekdays wK = weekdays.monday;
// make decision based
// on enum value
switch (wK)
{
case weekdays.sunday:
Console.WriteLine("the day isa sunday.");
break;
case weekdays.monday:
Console.WriteLine("The day is monday.");
break;
}
Console.ReadLine();
}
}
}
ArthematicOperators in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArthematicOperators
{
class Program
{
static void Main(string[] args)
{
int num1, num2;
int add, sub, mul;
float div;
//Accepting Values from users
Console.Write("Enter first number\t\t");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("\n\nEnter second number\t\t");
num2 = Convert.ToInt32(Console.ReadLine());
//Processing Values
//Used + operator for adding values
add = num1 + num2;
//Used - operator for subtracting values
sub = num1 - num2;
//Used * operator for multiplying values
mul = num1 * num2;
//Used / operator for dividing values
div = (float)num1 / num2;
//Displaying Output
Console.WriteLine("\n\n=====================\n");
Console.WriteLine("Addition\t\t{0}", add);
Console.WriteLine("Subtraction\t\t{0}", sub);
Console.WriteLine("Multiplication\t\t{0}", mul);
Console.WriteLine("Division\t\t{0}", div);
Console.WriteLine("\n=======================\n");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArthematicOperators
{
class Program
{
static void Main(string[] args)
{
int num1, num2;
int add, sub, mul;
float div;
//Accepting Values from users
Console.Write("Enter first number\t\t");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("\n\nEnter second number\t\t");
num2 = Convert.ToInt32(Console.ReadLine());
//Processing Values
//Used + operator for adding values
add = num1 + num2;
//Used - operator for subtracting values
sub = num1 - num2;
//Used * operator for multiplying values
mul = num1 * num2;
//Used / operator for dividing values
div = (float)num1 / num2;
//Displaying Output
Console.WriteLine("\n\n=====================\n");
Console.WriteLine("Addition\t\t{0}", add);
Console.WriteLine("Subtraction\t\t{0}", sub);
Console.WriteLine("Multiplication\t\t{0}", mul);
Console.WriteLine("Division\t\t{0}", div);
Console.WriteLine("\n=======================\n");
Console.ReadLine();
}
}
}
Multiple Cases. Falling Through!
<html>
<head>
<title></title>
</head>
<body>
<?php
$i = 5;
switch ($i) {
case 0:
echo '$i is 0.';
break;
case 1:
case 2:
case 3:
case 4:
case 5:
echo '$i is somewhere between 1 and 5.';
break;
case 6:
case 7:
echo '$i is either 6 or 7.';
break;
default:
echo "I don't know how much \$i is.";
}
?>
</body>
</html>
<head>
<title></title>
</head>
<body>
<?php
$i = 5;
switch ($i) {
case 0:
echo '$i is 0.';
break;
case 1:
case 2:
case 3:
case 4:
case 5:
echo '$i is somewhere between 1 and 5.';
break;
case 6:
case 7:
echo '$i is either 6 or 7.';
break;
default:
echo "I don't know how much \$i is.";
}
?>
</body>
</html>