Thursday, 20 February 2014

Stream Read and Write

class StreamReadWrite
    {
        public static void Main()
        {
            int rno;
            string name;

            Console.WriteLine("Enter Roll Number");
            rno = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Name");
            name = Console.ReadLine();
           

            FileStream fs = new FileStream("student.txt", FileMode.Append , FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);

           sw.WriteLine("My name is {0} and roll number is {1}", name, rno);
           

            sw.Close();
            fs.Close();


            FileStream fs1 = new FileStream("student.txt", FileMode.OpenOrCreate , FileAccess.Read);
            StreamReader sr = new StreamReader(fs1);

            string s;
            s = sr.ReadLine();
            Console.WriteLine("data in a file is \n {0}", s);
     
            s = sr.ReadLine();
            Console.WriteLine("data in a file is \n {0}", s);

            sr.Close();
            fs.Close();





            Console.WriteLine("Press any key to stop");
            Console.ReadKey();
        }

    }

Binary Reading and Writing in a File

class BinaryReadWrite
    {
       public static void Main()
        {
            int a;
            char b;
            double c;
            bool d = true;

             Console.WriteLine("Enter Int value");
              a = int.Parse(Console.ReadLine());
              Console.WriteLine("Enter Char value");
              b = char.Parse(Console.ReadLine());
              Console.WriteLine("Enter double value");
              c = double.Parse(Console.ReadLine());

              FileStream fs=new FileStream("File11to1.txt",FileMode.OpenOrCreate , FileAccess.Write,FileShare.ReadWrite);

              BinaryWriter bw = new BinaryWriter(fs);

              bw.Write(a);
              bw.Write(b);
              bw.Write(c);
              bw.Write(d);
              bw.Close();
              fs.Close();
             
            FileStream fs = new FileStream("File11to1.txt", FileMode.OpenOrCreate, FileAccess.Read );
            BinaryReader br = new BinaryReader(fs);

            a = br.ReadInt32();
            b = br.ReadChar();
            c = br.ReadDouble();
            d = br.ReadBoolean();


            Console.WriteLine("a={0}\nb={1}\nc={2}\nd={3}", a, b, c, d);


            Console.WriteLine("Press any key to stop");
            Console.ReadKey();

        }
    }

Monday, 17 February 2014

Method Returning More Than One Values using out

 struct Calculator
        {

            void calculate(int a, int b, out int p, out int q, out int r, out int s)
            {


               p = a + b;
                q = a - b;
                r = a * b;
                s = a / b;

                Console.WriteLine("All operations PERFORMED successfully");


            }
            static void Main()
            {
                int a, b,c,d,e,f;

                int[] r = new int[5];
                Console.WriteLine("Enter First No");
                a = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter Second No");
                b = int.Parse(Console.ReadLine());

                Calculator o = new Calculator();
                o.calculate(a, b, out c, out d, out e, out f);


                Console.WriteLine("Sum={0}", c);
                Console.WriteLine("Subtraction={0}", d);
                Console.WriteLine("Multiplication={0}", e);
                Console.WriteLine("Division={0}", f);

                Console.WriteLine("Press Any Key To Exit");
                Console.ReadLine();
            }
        }

Method Returning Multiple Values

        struct Calculator
        {

            int[] calculate(int a, int b)
        {
            int[] result = new int[5];
          
            result[0] = a + b;
            result[1] = a - b;
            result[2] = a * b;
            result[3] = a / b;

            Console.WriteLine("All operations PERFORMED successfully");
            return result;
              
        }
            static void Main()
            {
                int a, b;

                int[] r = new int[5];
                Console.WriteLine("Enter First No");
                a = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter Second No");
                b = int.Parse(Console.ReadLine());

                Calculator o = new Calculator();
               r= o.calculate(a, b);


                Console.WriteLine("Sum={0}",r[0]);
                Console.WriteLine("Subtraction={0}",r[1]);
                Console.WriteLine("Multiplication={0}",r[2]);
                Console.WriteLine("Division={0}",r[3]);

                Console.WriteLine("Press Any Key To Exit");
                Console.ReadLine();
            }
        }

Function Having return type

   struct Square
        {
            int SquareMe(int x)
            {
       
                int s;
            
                Console.WriteLine ("I am Squaring You");
                s = x * x;
                return s;
            }


           static void Main()
            {
                int n, s1=0;
                Square o = new Square();

                for (int i = 1; i <= 5; i++)
                {
                    Console.WriteLine("Enter Any Number");
                    n = int.Parse(Console.ReadLine());
                    s1 = o.SquareMe(n);
                    Console.WriteLine("Square of {0} is {1}", n, s1);
                   
                }
               
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }

Pass by Reference using out

   struct Increment
        {
            void increment(out int n)
            {
                n = 2;
                n = n + 1;
                Console.WriteLine("\n****in method my value is {0}", n);
            }

        static void Main()
            {
                int n ;
                Console.WriteLine("Enter any no");
               //  n = int.Parse(Console.ReadLine());
                //Console.WriteLine("\nBefore Calling method...My Value is {0}", n);
                Increment o = new Increment();
                o.increment(out n);
                Console.WriteLine("\nAfter Calling method....My Value is {0}", n);
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }

Pass by reference

 struct Increment
        {
            void increment(ref int n)
            {
                n = n + 1;
                Console.WriteLine("\n****in method my value is {0}", n);
            }
            static void Main()
            {
                int n;
                Console.WriteLine("Enter any no");
               n = int.Parse(Console.ReadLine());
                Console.WriteLine("\nBefore Calling method...My Value is {0}", n);
                Increment o = new Increment();
                o.increment(ref n);
                Console.WriteLine("\nAfter Calling method....My Value is {0}", n);
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }

Pass By Value


Passing a value-type data to a method by value means passing a copy of the variable to the method.

Any changes to the parameter that take place inside the method have no affect on the original data in the calling method.


  struct Square
        {
            void SquareMe(int x)
            {
                int s;
                s = x * x;
                Console.WriteLine("Square of {0} is {1}", x, s);
            }

            void increment(int n)
            {
                n = n + 1;
                Console.WriteLine("\n****in method my value is {0}", n);
            }

            static void Main()
            {
                int n;
                Console.WriteLine("Enter Any Number");
                n = int.Parse(Console.ReadLine());
                Console.WriteLine("\nBefore Calling method...My Value is {0}", n);
                Square o = new Square();
                o.increment(n);
                Console.WriteLine("\nAfter Calling method....My Value is {0}", n);
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }