Sponsored Ad

Monday, January 17, 2011

Stringwriter Class in C# – Example Code

This example demostrate you that how to use StringWriter class in C#. The given program trying to write string using StringWriter class.

StringReader and Stringwriter Class in C#

C# Source Code:

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

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append("StringBuilder.");
            StringWriter strw = new StringWriter(strb);
            Writetxt(strw);
            Console.WriteLine(strb);

        Console.ReadLine();
          }
        static void Writetxt(TextWriter tw)
        {
            tw.WriteLine("TextWriter.");
        }
    }
}

How to Copy an Array into Another Array using C# | Array.Copy()

This sample code is to copy and array into another array using the C# inbuilt method. This method also allow you to specify number of elements to copy in another array. The full code is given below.

Please note that before copying the array you need to create the target array.

How to Copy an Array into Another Array using C# | Array.Copy()

using System;

namespace MyApp
{
    class clsCopyArray
    {
        static void Main(string[] args)
        {
            string[] strArray_Source = { "A", "B", "C", "D", "E", "F", "G", "H", "I"};
            string[] strArray_Target = new string[5];
            Console.WriteLine("Source Array: ");
            foreach (string str in strArray_Source)
                Console.Write(str + " ");

            Console.WriteLine("");

            Console.WriteLine("Target Array: ");
            Array.Copy(strArray_Source, strArray_Target, 5);
            foreach (string str in strArray_Target)
                Console.Write(str + " ");
            Console.ReadLine();
          }         
    }
}

Find an Element using Binary Search in an Array - C#

Binary Search is possible in an array. You do not have to write the customer code to search an element. C# provides a inbuilt function to search an element using the binary search. The sample example of binary search is given below.

Find an Element using Binary Search in an Array - C#

using System;

namespace MyApp
{
    class clsBinarySearch
    {
        static void Main(string[] args)
        {
            string[] strArray = { "A", "B", "C", "D", "E", "F", "G", "H", "I"};
            Console.WriteLine("Source Array: ");
            foreach(string str in strArray)
                Console.Write(str + " ");

            Console.WriteLine("");
           int intIndex = Array.BinarySearch(strArray, "E");
           Console.WriteLine("Element 'E' Found at: " + intIndex);
            intIndex = Array.BinarySearch(strArray, "e");
           Console.WriteLine("Element 'e' Found at: " + intIndex);

            Console.ReadLine();
          }         
    }
}

How to Reverse an Array using C# Method

C# provide a inbuilt method to reverse the given array in a efficient way. You can use this med and just pass the array into this function by reference and the array will converted in reverse order. The following example will help you to understand the use of array.reverse method.

How to Reverse an Array using C# Method

using System;

namespace MyApp
{
    class ArrayReverse
    {
        static void Main(string[] args)
        {
            string[] strArray = { "ten", "nine", "eight", "seven" };
            Console.WriteLine("Before Reverse: ");
            foreach(string str in strArray)
                Console.Write(str + " ");
            Array.Reverse(strArray);

            Console.WriteLine("");
            Console.WriteLine("After Reverse: ");
            foreach (string str in strArray)
                Console.Write(str + " ");

            Console.ReadLine();
          }         
    }
}

Sort an Array using Inbuilt Array Functions in C#

Do you have a array and would like to sort the array. C# provide a inbuilt function to sort the array. You need to call Array.Sort() function and pass the array as an parameter to it. This calling is by reference and the sort function will change the original array. once you print the array it will be in sorted order.

Sort an Array using Inbuilt Array Functions in C#

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strArray = { "one", "two", "three", "four" };
            Console.WriteLine("Before Sorting: ");
            foreach(string str in strArray)
                Console.Write(str + " ");
            Array.Sort(strArray);

            Console.WriteLine("");
            Console.WriteLine("After Sorting: ");
            foreach (string str in strArray)
                Console.Write(str + " ");

            Console.ReadLine();
          }         
    }
}

Monday, January 10, 2011

Find the Rectangle Area in C#

This C# code will help you to find the rectangle are using the C#. You need to just multiply the height and width of rectangle.

            dArea = dHeight * dWidth;

Find the Rectangle Area in C#

Source Code of rectangle Area calculation :

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            double dWidth, dHeight;
            double dArea;
            Console.WriteLine("Enter the both side of rectangle (Height and Width):");
            dHeight = Convert.ToDouble(Console.ReadLine());
            dWidth = Convert.ToDouble(Console.ReadLine());
            dArea = dHeight * dWidth;
            Console.WriteLine("The area of rectangle is: " + dArea);
            Console.ReadLine();
        }    
    }
}

Calculate Area of Square using C#

This Simple program will help you to find the area of a given Square . As the mathematics formula says, the area should be Square of a side.

      dArea = dLength * dLength;

Calculate Area of Squire using C#

Source Code:

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            double dLength;
            double dArea;
            Console.WriteLine("Enter the length of a side of Square :");
            dLength = Convert.ToDouble(Console.ReadLine());
            dArea = dLength * dLength;
            Console.WriteLine("The area of Square is: " + dArea);
            Console.ReadLine();
        }    
    }
}

How to Calculate Circumference of Circle in C#

The following program will calculate circumference of circle for a given radius. if you have a diameter of circle use:

dCircumference = 3.14 * diameter ;

This is simple C# program. Please let us know if you face any problem while implementing.

How to Calculate Circumference of Circle in C#

Source Code of Circle Perimeter:

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int iRadius;
            double dCircumference;
            Console.WriteLine("Enter the radius of given circle:");
            iRadius = Convert.ToInt32(Console.ReadLine());
            dCircumference = 2 * 3.14 * iRadius;
            Console.WriteLine("The circumference of a circle is: " + dCircumference);
            Console.ReadLine();
        }    
    }
}

How to Calculate Area of Circle using C#

The given program will help you to find out the area of circle. This C# program using the simple formula of math's to calculate the area of circle.

How to Calculate Area of Circle using C#

Code:

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int iRadius;
            double dArea;
            Console.WriteLine("Enter the radius:");
            iRadius = Convert.ToInt32(Console.ReadLine());
            dArea = 3.14 * iRadius * iRadius;
            Console.WriteLine("The Area of circle is: " + dArea);
            Console.ReadLine();
        }    
    }
}

Sponsored Ad

Development Updates