Skip to main content

Posts

Showing posts from December, 2014

Generics Example in C#

using System; namespace Generics { public class MyGeneric<T> {   //type of the class is ommited private T[] array; public MyGeneric(int size) { array = new T[size + 1]; } public T getItem(int index) { return array[index]; } public void setItem(int index, T value) { array[index] = value; } } class Program { static void Main(string[] args) { //declare double array MyGeneric<double> doubleArray = new MyGeneric<double>(5); //set values double x; for (int c = 0; c < 5; c++) { x = c * 10; doubleArray.setItem(c, Convert.ToDouble(x)); } //get values for (int a = 0; a < 5; a++) { Console.WriteLine(doubleArray.getItem(a)); } Console.ReadLine(); } } }

Equals in Object Class (Comparing objects)

using System; namespace Object_Class_ToString { class Emp { int id; string name; public int Id { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } //public override string ToString() //{ //    return string.Format("{0} - {1}", id,name); //} } class Program { static void Main(string[] args) { Emp e1 = new Emp(); Emp e2 = e1; Console.WriteLine(e1.Equals(e2)); //will return true since both refering to same object Emp e3 = new Emp();//new Object e2 = e3; Console.WriteLine(e1.Equals(e2)); //will return false they are refering to different objects Console.ReadLine(); } } }

Redistribute RIP routes

!Redistribution is doing where when we need to pump our routes to different protocol !RIP redistribution is available for following protocols in Cisco IOS 15 Router(config-router)#redistribute ? connected  Connected eigrp      Enhanced Interior Gateway Routing Protocol (EIGRP) metric     Metric for redistributed routes ospf       Open Shortest Path First (OSPF) rip        Routing Information Protocol (RIP) static     Static routes !RIP to EIGRP Router(config-router)#redistribute eigrp <AS> metric <default-matric> !RIP to OSPF Router(config-router)#redistribute ospf <process-ID> metric <default-metric>

Delegates in C#

comes under System.delegates type of variable which keeps references to methods defines return type and parameter list Example: class Program { //declaring delegate delegate double calc(num1,num2); public double addition(num1,num2)//same signature {return num1+num2;} public double minus(num1,num2)//same signature {return num1-num2;} } class MainClass { private static void main(string[] args) { calc delObj;//created object with delegate string input=Console.readLine(); if(input=='Add') { //instantiate delegate with reference to addition delObj=new delObj(addition); } else { //instantiate delegate delObj=new delObj(minus); } //call delegate delObj(10,20) } }

Stored procedures sample program

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace Stored_Proc { public partial class WebForm1 : System.Web.UI.Page { //DataSet ds = new DataSet(); SqlConnection con; SqlCommand cmd = new SqlCommand(); //SqlParameter sp1 = new SqlParameter(); //SqlParameter sp2 = new SqlParameter(); //SqlParameter sp3 = new SqlParameter(); //SqlParameter sp4 = new SqlParameter(); protected void Page_Load(object sender, EventArgs e) { Label6.Text = null; doSomething(); } protected void Button1_Click(object sender, EventArgs e) { con = new SqlConnection("Data Source=KUSHANLAHIRU-PC;Initial Catalog=example1;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"); cmd = new SqlCommand("Splogin", con); cmd.CommandType = CommandType.Sto