Wednesday, August 22, 2012

How to use Operator Overloading in C#?


In this article, we will discuss operator overloading in C#. It means that we are defining an operator for a class. It is very like method overloading. We can declare it with the help of the operator keyword like this:
public static calculation operator /(calculation op1, calculation op2){}
Here we define an operator (/) with the operator keyword.
Declaration: We can generally declare it this way:

For Unary:
public static ret-type operator  op(parameter-type operand){}
For Binary
public static ret-type operator  op(parameter-type operand1, parameter-type operand1){}
Example:

First we look at an example of a unary operator(++):
class calculation{   int a, b, c;   public calculation()   {       a = b = c = 0;   }   public calculation(int x, int y, int z)   {       a = x;       b = y;       c = z;   }    public static calculation operator ++(calculation op1)   {       op1.a++;       op1.b++;       op1.c++;       return op1;   }   public void ShowTheResult()   {       Console.WriteLine(a + "," + b + "," + c);       Console.ReadLine();   }}class Program{    static void Main(string[] args)    {        calculation i = new calculation(10, 20, 30);        calculation j = new calculation(5, 10, 3);        calculation k = new calculation();        i++;        i.ShowTheResult();        Console.WriteLine();    }}

Here we use the ++ operator so the values 10, 20, 30 will be 11, 21, 31.

Now we look at an example of a binary operator; here we do the calculations like (+,-,*,/) with the help of operator overloading.

Here we create a Class Calculation in our program like this:
class calculation{     int a, b, c;     public calculation()     {         a = b = c = 0;     }     public calculation(int x, int y, int z)     {         a = x;         b = y;         c = z;     }     public static calculation operator +(calculation op1, calculation op2)     {         calculation calc = new calculation();         calc.a = op1.a + op2.a;         calc.b = op1.b + op2.b;         calc.c = op1.c + op2.c;         return calc;    }    public static calculation operator -(calculation op1, calculation op2)    {         calculation calc = new calculation();         calc.a = op1.a - op2.a;         calc.b = op1.b - op2.b;         calc.c = op1.c - op2.c;         return calc;    }    public static calculation operator *(calculation op1, calculation op2)    {         calculation calc = new calculation();         calc.a = op1.a * op2.a;         calc.b = op1.b * op2.b;         calc.c = op1.c * op2.c;    }    public static calculation operator /(calculation op1, calculation op2)    {         calculation calc = new calculation();         calc.a = op1.a / op2.a;         calc.b = op1.b / op2.b;         calc.c = op1.c / op2.c;         return calc;    }  
    public void ShowTheResult()    {         Console.WriteLine(a + "," + b + "," + c);         Console.ReadLine();    }}
Here we define the operators and a function (ShowTheResult()) to show the output. After that we write the following code in the Program class. By this the addition will be performed and the result will be shown with the help of The ShowTheResult() function.
 
class Program{    static void Main(string[] args)    {        calculation j = new calculation(5, 10, 3);        calculation k = new calculation();        k = i + j;        k.ShowTheResult();        Console.WriteLine();    }}

For subtraction, multiplication and division we write the following code:
class Program{     static void Main(string[] args)     {         calculation i = new calculation(10, 20, 30);         calculation j = new calculation(5, 10, 3);         calculation k = new calculation();         k = i - j;         k.ShowTheResult();         Console.WriteLine();               }}
Multiplyclass Program{
     static void Main(string[] args)
     {
         calculation i = new calculation(10, 20, 30);
         calculation j = new calculation(5, 10, 3);
         calculation k = new calculation();
         k = i * j;
         k.ShowTheResult();
         Console.WriteLine();
     }
 }
Division
class Program{     static void Main(string[] args)     {         calculation i = new calculation(10, 20, 30);         calculation j = new calculation(5, 10, 3);         calculation k = new calculation();         k = i / j;         k.ShowTheResult();         Console.WriteLine();    }
}


Checking the availability of a word in a sentence ?


Here in this post I have given an example that shows how to check the availability of a word in a given sentence using C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace CheckAvailability
{
    class Program
    {
        static void Main(string[] args)
        {

            string data = "Prahalad is the king of Programming ";
            Console.WriteLine("{0}\n\n",data);

            Console.WriteLine("Enter a word to check the availability from the above sentence\n");
            string get = Console.ReadLine();
            bool result = data.Contains(get);
            if (result == true)
            {
                Console.WriteLine("Data {0} is available",get);
            }
            else
                Console.WriteLine("Data {0} is not available",get);

            Console.Read();
        }
    }
}

thank you

What is WPF?


Windows Presentation Foundation (WPF) is the code-name of the presentation (user-interfaces) sub system in Windows programming model and is used to create user interfaces. This blog talks about WPF.
If you have been programming .NET, you must be familiar with Windows Forms and ASP.NET. Windows Forms are used to build Windows client application and ASP.NET is used to build Web applications.
Well, WPF is a new technology that may be used instead of both Windows Forms and ASP.NET.
What is WPF?
WPF is the engine that is responsible for creating, displaying, and manipulating user-interfaces, documents, images, movies, and media in Windows Vista.
Physically, WPF is a set of libraries that have all functionality you need to build, run, execute, and manage Windows Vista applications.
What is XAML?
XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next generation managed applications.
How XAML is related to WPF?
XAML is a new descriptive programming language developed by Microsoft to write user interfaces for next generation managed applications. XAML is used in WPF to represent the controls and code with the help of C#, Visual Basic, and other .NET Framework languages.

XAML can be think as ASP.NET and/or Windows Forms in Windows Vista. For example, to write a Web application in .NET 1.0, 1.1, or 2.0, you use ASP.NET and to write Windows Applications, you use Windows Forms. Now in Windows Vista and .NET 3.0, you will use XAML instead of Windows Forms and ASP.NET.

Does that mean XAML will replace ASP.NET and Windows Forms? YES and NO. Both ASP.NET and Windows Forms will also be supported on .NET 3.0 but you don't have to use them if you don't want.
What Operating Systems does WPF support?
Windows Vista, Windows XP, and Windows 2003 Server.
How do I build WPF Applicaitons?
To build WPF application, you must install .NET 3.5 SDK. You can also also Visual Studio 2010 and 2008. These two comes with WPF. If you are a student or beginner, you may want to try free version of Visual C# Express 2008 here.

Alternatively, you can download other versions on MSDN.
What do I need to run WPF Applications?
To run WPF applications, you must install .NET 3.5 or 4.0 SDK redistributable. It can be found on MSDN downloads sites.

How to use GZipStream ??



Here i will give you one examples which implements GZipstream class.

You just go through the program which is written in C# language


using System.IO;
using System.IO.Compression;
using System.Text;

class Program
{
    static void Main()
    {
 try
 {
     // 1.
     // Starting file is 26,747 bytes.
     string anyString = File.ReadAllText("TextFile1.txt");

     // 2.
     // Output file is 7,388 bytes.
     CompressStringToFile("new.gz", anyString);
 }
 catch
 {
     // Couldn't compress.
 }
    }

    public static void CompressStringToFile(string fileName, string value)
    {
 // A.
 // Write string to temporary file.
 string temp = Path.GetTempFileName();
 File.WriteAllText(temp, value);

 // B.
 // Read file into byte array buffer.
 byte[] b;
 using (FileStream f = new FileStream(temp, FileMode.Open))
 {
     b = new byte[f.Length];
     f.Read(b, 0, (int)f.Length);
 }

 // C.
 // Use GZipStream to write compressed bytes to target file.
 using (FileStream f2 = new FileStream(fileName, FileMode.Create))
 using (GZipStream gz = new GZipStream(f2, CompressionMode.Compress, false))
 {
     gz.Write(b, 0, b.Length);
 }
    }
}

Result
    Starting file is 26,747 bytes.
    Output file is 7,388 bytes.