Asp.Net

Tuesday, 12 August 2014

Useful C# Codes



Reverse a string in C#

namespace ReverseString
{
    public class Program
    {
        public  static string  stringreverse(string s)
        {
            char[] arr=s.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
        static void Main(string[] args)
        {
            Console.WriteLine(Program.stringreverse("naveen"));
            Console.ReadLine();
        }
    }
  
}
Fibbonacci series
class Program
    {
        static void Main(string[] args)
        {
            int a = 0, b = 1, c, n,i;
Console.WriteLine("Enter the range upto which the fibbonacci series has to print");
            n = int.Parse(Console.ReadLine());
            Console.WriteLine("The Fibbonacci Series  are {0} \t,{1}", a, b);
        
            for (i =2; i < n; i++)
            {
                c = a + b;
                Console.Write("\t {0}", c);
             
                a = b;
                b = c;
               
            }
          
            Console.ReadKey();

        }
    }

 


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

namespace ConsoleApplication2
{
    class Program
    {
        public delegate int del_pod(int a, int b);
        static int fn_mul(int a, int b)
        {
            return a * b;
        }
        static void Main(string[] args)
        {
            //simple array reverse
            string[] arr = new string[2];

            Console.WriteLine("Enter the Array Elements");
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = Console.ReadLine();
            }
            Console.WriteLine("The Array elements are {0},{1}", arr);

            arr.Reverse();


            Console.WriteLine("The array elemets after reversing the elements are {0},{1}", arr);
            Console.ReadKey();

           // generics class demo
            generics<int> test1 = new generics<int>(2);
            test1.write();

            generics<string> test2 = new generics<string>("Naveen");
            test2.write();

            Console.ReadLine();

            //generic method demo
            SAMPLE S1 = new SAMPLE();
            S1.Show<int>(5);
            S1.Show<string>("NAVEEN");
            S1.Show<bool>(true);
            Console.ReadLine();

            SAMPLE s2 = new SAMPLE();

            string d = s2.dateformats<string>("2014-06-20");
            string n = s2.dateformats<DateTime>(DateTime.Now);

            Console.WriteLine(d);
            Console.WriteLine(n);

            Console.ReadKey();
            //interface demo
            display s = new display();
            s.show();
            s.show1();
            Console.ReadLine();
           // even number linq

            var result = from r in
                             Enumerable.Range(1,10).Where(num => num % 2 == 0)
                         select r;
            foreach (var r in result)
                Console.WriteLine(r);
            Console.ReadKey(true);



           // delegate demo
            del_pod delob = new del_pod(fn_mul);
            Console.Write("Enter the two numbers");
            int v1 = Int32.Parse(Console.ReadLine());
            int v2 = Int32.Parse(Console.ReadLine());
            int res = delob(v1, v2);
            Console.WriteLine("the product of two numbers is:" + res);
            Console.ReadLine();



        }
    }
        class generics<T>
        {
            T nav;
            public generics(T a)
            {
                this.nav = a;
            }

            public void write()
            {
                Console.WriteLine(this.nav);
            }

        }

        class SAMPLE
        {
            public string  dateformats<TypeOfValue>(TypeOfValue val)
            {
                    string Convertdt;
                    DateTime dtformat = Convert.ToDateTime(val);
                    return (Convertdt = string.Format("{0:dd-MM-yyyy}", dtformat));
            }
            public void Show<T>(T val)
            {
                Console.WriteLine(val);
            }
        }

        public interface display1
        {
            void show();
        }
        public interface display2
        {
           void show1();
        }
        class display : display1, display2
        {
            public void show()
            {
                Console.WriteLine("Hello Naveen");

            }
            public void show1()
            {
                Console.WriteLine("You are doing good job");
            }
        }
}

No comments:

Post a Comment