Saturday, May 17, 2014

C#

OOP Properties

Abstraction

Hiding the implementation and providing services to user is known as Abstraction. 
Polymorphism

Polymorphism means “Many forms”, When one method behaves in different forms it is known as Polymorphism. Also, new implementation of same method with different signature is known as Function Overloading. There are the following two types of Polymorphism.
  • Static Polymorphism
  • Dynamic Polymorphism

Func

http://msdn.microsoft.com/en-us/library/bb549151.aspx

Func<X, Y, Z> translated to English is: “A method that takes an X, and a Y as parameters and returns a Z”.”
using System;

class Program
{
    static void Main()
    {
 //
 // Create a Func instance that has one parameter and one return value.
 // ... Parameter is an integer, result value is a string.
 //
 Func<int, string> func1 = (x) => string.Format("string = {0}", x);
 //
 // Func instance with two parameters and one result.
 // ... Receives bool and int, returns string.
 //
 Func<bool, int, string> func2 = (b, x) =>
     string.Format("string = {0} and {1}", b, x);
 //
 // Func instance that has no parameters and one result value.
 //
 Func<double> func3 = () => Math.PI / 2;

 //
 // Call the Invoke instance method on the anonymous functions.
 //
 Console.WriteLine(func1.Invoke(5));
 Console.WriteLine(func2.Invoke(true, 10));
 Console.WriteLine(func3.Invoke());
    }
}

Output

string = 5
string = True and 10
1.5707963267949
 We stored anonymous functions inside Func pointers and then invoked those methods through the Invoke method on the Func instances.
The Action type receives parameters but does not return a parameter. Func receives parameters and returns a result value.
So:An Action instance never returns anything, while the Func always returns something.

C# Action

using System;

class Program
{
    static void Main()
    {
 // Example Action instances.
 // ... First example uses one parameter.
 // ... Second example uses two parameters.
 // ... Third example uses no parameter.
 // ... None have results.
 Action<int> example1 =
     (int x) => Console.WriteLine("Write {0}", x);
 Action<int, int> example2 =
     (x, y) => Console.WriteLine("Write {0} and {1}", x, y);
 Action example3 =
     () => Console.WriteLine("Done");
 // Call the anonymous methods.
 example1.Invoke(1);
 example2.Invoke(2, 3);
 example3.Invoke();
    }
}

Output

Write 1
Write 2 and 3
Done
http://www.dotnetperls.com/func
http://www.growingwiththeweb.com/2012/08/func-and-action-basics-in-c.html




Timer

Timer Example ; Auto  Fire after 3 seconds...........................

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.Timers;

namespace WindowsFormsApplication1
{
  
    public partial class Form1 : Form
    {
       private System.Timers.Timer timer;
       private int counter = 0;

       public Form1()
       {
        
           InitializeComponent();
           timer = new System.Timers.Timer();
           timer.Elapsed += timer_Elapsed;
           timer.Interval = (1000 * 3);
           timer.Start();
       }
       void timer_Elapsed(object sender, ElapsedEventArgs e)
       {
           counter = counter + 1;
       
               timer.Stop();
             MessageBox.Show(counter.ToString());
               timer.Start();
         
       }
    }
}
http://www.codeproject.com/Tips/480049/Shut-Down-Restart-Log-off-or-Lock-your-computer-in

1 comment:

  1. Asynchronous Programming with Async and Await
    http://msdn.microsoft.com/en-us/library/hh191443.aspx

    ReplyDelete