Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Saturday, December 29, 2012

What are the 5 uses of delegate

1. Most important use of delegates- it helps us to define an abstract pointer which can point to methods and functions. The same abstract delegate can be later used to point to that type of functions and methods.

2. Callback mechanism Many times we would like to provide a call back mechanism. Delegates can be passed to the destination and destination can use the same delegate pointer to make callbacks.

3. Asynchronous processing By using ‘BeginInvoke’ and ‘EndInvoke’ we can call delegates asynchronously.

4. Multicasting - Sequential processing Some time we would like to call some methods in a sequential manner which can be done by using multicast delegate.

5. Events - Publisher subscriber modelWe can use events to create a pure publisher / subscriber model.


Code Project uses of delegate click here

Thursday, December 20, 2012

What happen if we have 2 return statement in a function

//Program in cpp using 2 return statement in function
#include <iostream>
using namespace std;

int add ()
{
    return 2;
    return 3;
}

int main()
{
    cout << add();
}
  • If  we compiler this program using GCC compiler, it would not give any error or warning.
  • But when we compile this program in Dotnet it give warning message.
___________________________________________________________________________________
//Program in sql using 2 return statement in function
create function ab()
returns int
as
    begin
        return 100
        return 120
    end
   
print dbo.ab()

This is not give the error because the sql compiler is not enough smart

MORAL of the story: it totally dependent on the compiler, that it gives warning or not, when we use 2 return statement in a function.

Wednesday, December 19, 2012

program to show; Dotnet provide cross language environment

here is the example which shows how we can achieve cross language environment in dotnet
1. First we make a dll in VB.net
2. and after that we use this VB.net's dll in c# (here we achieving cross language environment)

lets try it:
program to making dll in vb.net

//we are saving this program as SampleVBDll.vb
imports System

public class test
    public function show_data() as string
        return "hello Csharp"
    end function
end class


Complie this as:   c:\>vbc /t:library SampleVBDll.vb     (this is command to making a dll in vb.net)

and it gives us "SampleVBDll.dll" later on we use this dll as reference while compling the csharp program


//in this program we are using the VB .net dll in csharp program
//This program name is 'demo.cs'
using System;

class demo:test      //here we inherit class test in our class demo
{
    public static void Main()
    {
        test t = new test();       //here i am making the object of test class which is in 'SampleVBDll'
     

       string f = t.show_data();  //and here we are calling the function(show_data) of class 'test' which
                                           // is in 'SampleVBDll'
        Console.WriteLine(f);
    }
}


Compile this as:   c:\>csc /r:SampleVBDll.dll demo.cs 

and this give us 'demo.exe'   and output is below
OUTPUT:
hello Csharp



Tuesday, December 4, 2012

A simple 'property' program

What is properties:
  • Properties are like data fields, but have logic behind them
  • From the outside, they look like any other member variable
              But they act like a member function
  • Defined like a field, with 'get' and 'set' code added
  • Can use access modifiers like fields and methode can

Why use Properties?

  • properties can be calculated on-the-fly
  • provide complete control over data field access
            logic is hidden from the object's consumer
  • Properties promote the idea of encapsulation

 //A simple program to implement property
//using c#

using System;

class company
{
    public void interview_sch()
    {
        Console.WriteLine ( "your interview is schedule" );
    }
}

class candidate
{
    private string skills;
   
    public string can_skills                // This
    {                                             // is
        get                                      // 
        {                                         // P
            return skills;     //retrieve    // R
        }                                         // O
                                                   // P
        set                                      // E
        {                                        // R
            skills = value;   //assign      // T
        }                                        // Y
    }                                            //
}

class consultant
{
    public static void Main()
    {
        candidate anil = new candidate();
        anil.can_skills = "java";
        string s = anil.can_skills;
       
        if (s == "c")
        {
            company c = new company();
            c.interview_sch();
        }
       
        else
        {
            Console.WriteLine ( "your criteria deoesn't match" );
        }
    }
}

Wednesday, November 21, 2012

Simple Thread Programming in C#

// creating and starting threads
using System;
using System.Threading;
public class AOne
{
    public void First()
    {
        Console.WriteLine("First methode of AOne class is running on T1 thread.");
        Thread.Sleep(1000);
        Console.WriteLine("The First method called by T1 thread has ended.");
    }
   
    public static void Second()
    {
        Console.WriteLine("First methode of AOne class is running on T1 thread.");
        Thread.Sleep(2000);
        Console.WriteLine("The First method called by T1 thread has ended.");
    }   
}

public class ThreadExp
{
    public static int Main(string[] args)
    {
        Console.WriteLine("Example of Threading");
        AOne a = new AOne();
        Thread T1 = new Thread(new ThreadStart(a.First));  //creating thread T1
        T1.Start();    //Strating thread T1
        Console.WriteLine("T1 thread  started.");
        Thread T2 = new Thread(new ThreadStart(AOne.Second));  //creating thread T2
        T2.Start();    //starting thread T2
        Console.WriteLine("T2 thread started.");
        return 0;
    }
}

_____________________________________________________________________________________

// 2nd Program------using timer
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;

namespace ThreadTimers
{
    class Program
    {
        static void Main()
        {
            Timer time = new Timer();  //Doesn't require any args
            time.Interval = 1000;
            time.Elapsed += time_Elapsed;    //uses an event
            time.Start();      //start the timer
            Console.ReadLine();
            time.Stop();
            Console.WriteLine("Timer Thread stopped. Enter again to start. \n Twice entering would

                                                               destroy the thread.");              //pause the timer
            Console.ReadLine();
            time.Start();           //Resume the timer
            Console.ReadLine();
            Console.WriteLine("Thread destroyed!");
            time.Dispose(); //Destroy the timer Thread
        }
       
        static void Display(object ObjTime)
        {
            //This runs on a pooled thread            
            Console.WriteLine(ObjTime);            //Writes "tick..."
        }
       
        static void time_Elapsed (object sender, EventArgs e)
        {
            Console.Write("The Time is passing by: ");
            Console.WriteLine(DateTime.Now);
        }
    }
}

Tuesday, October 16, 2012

Difference and making of namespace in c++ and c#

//How to write a program for namespace in visual c++

#include"StdAfx.h"
#include<iostream>
using namespace std;

namespace uptu
{
    class abes
    {
        private:
            int x;
           
        public:
          void assign() {
            x=100;
          }
         
          void show() {
            cout<<x; 
          }
    };
   
    class akeg
    {
        private:
            int x;
           
        public:
            void assign() {
                x=121;
            }
           
            void show() {
                cout<<x;
            }
    };
}

int  main()

    {
        uptu::abes a;
        a.assign();
       
        uptu::akeg b;
        b.assign();
        b.show();

        return 0;
    }
___________________________________________________________________
 // namespace in csharp
// must be added at beginning


using System;
using y=india.uptu;

namespace india
{
    namespace uptu
    {
        public class abes
        {
            public void show()
            {
                Console.WriteLine("This is alias");
            }
 
            public static void Main()
            {
                y.abes a=new y.abes();
                a.show();
            }
        }
    }
}

Friday, April 20, 2012

A simple program to understand classes in C#

using System;

class building
{
    public int floors;   //number of floors         //C# class doesn't support 'public :'
    public int area;     //total square footage of building
    public int occupants;  //number of occupants
}

class building_Demo
{
    public static void Main()
    {
        building house = new building();  //create a bbuilding object
        int areaPP;    //area per person
      
        //assign values to fields to fields in house
        house.occupants = 4;
        house.area = 2500;
        house.floors = 2;
  
    //compute the area per person
    areaPP = house.area/house.occupants;
  
    Console.WriteLine("house has:\n "+
                       house.floors + " floors\n "+
                       house.occupants + " occupants\n "+
                       house.area + " total area\n "+
                       areaPP + " area per person");
    }
}


OUTPUT:
 house has:
  2  floors
  4  occupants
  2500  total area
  625 area per person

Monday, April 2, 2012

Program to illustrate 'const' keyword

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable cannot be modified.

// program to illustrate const keyword
// using  c#


using System;

public class ConstTest
{
    class MyClass
    {
        public int x;
        public int y;
        public const int c1 = 5;
        public const int c2 = c1 + 5;
       
        public  MyClass(int p1, int p2)
        {
            x = p1;
            y = p2;
        }
    }
   
    public static void Main()
    {
        MyClass mC = new MyClass(11, 22);
        Console.WriteLine ("x = {0}, y = {1}", mC.x, mC.y);
        Console.WriteLine ("c1 = {0}, c2 = {1}", MyClass.c1, MyClass.c2);
    }
}

OUTPUT:
x = 11, y = 22
c1 = 5, c2 = 10