Tuesday, December 16, 2014

Understanding Generics


Understanding Generics:

Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.




namespace Apps
{
    class UnderstandingGenerics
    {
        static void GenericsTest<T>(T lhs, T rhs)
        {
            Console.WriteLine("{0}, {1}", lhs, rhs);
        }
        static void Main(string[] args)
        {
            int a = 10, b = 2;
            Char c = 'r', d = 's';
            GenericsTest<int>(a, b);
            GenericsTest<char>(c, d);
        }
    }
}

Thursday, September 25, 2014

HyperLinks in text file C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HyperLinksFilter
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                StreamReader sfilecontent1 = new StreamReader(@"D:\log");
                StreamWriter dfilecontent2 = new StreamWriter(@"D:\test.txt");
                string line;
                while ((line = sfilecontent1.ReadLine()) != null)
                {
                    //string[] words = line.Split(',');
                    //string k = words[1];
                    //dfilecontent2.Write(k.Trim() + "\r\n");

                    if (line.Contains("http"))
                    {
                        dfilecontent2.Write(line+"\r\n"+"\r\n");
                    }
                }
                sfilecontent1.Close();
                dfilecontent2.Close();
                Console.WriteLine("File created Successfully!");
            }
            catch (Exception ex1)
            {
                Console.WriteLine(ex1.Message);
            }
        }
    }
}

Tuesday, August 26, 2014

Understanding Static Constructor (C# reference)

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

namespace Apps
{

    class statiConstructor
    {
        private static int a;
        static statiConstructor()
        {
            a = 10;
            Console.WriteLine("Static Constructor with static member value=" + a);
        }
        public static void MethodCall()
        {
            Console.WriteLine("Method Call ");
        }
    }
    class UnderstandingStaticConstructor
    {
        public static void Main()
        {
         // In particular, a static constructor is called once, automatically, when the class is used for the first time.
            statiConstructor k = new statiConstructor();

            statiConstructor.MethodCall();
            statiConstructor.MethodCall();

        }
    }
}



Output:

Static Constructor with static member value = 10
Method Call
Method Call



Understanding Indexer in C#

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

namespace Apps
{
    class IndexerExample
    {
        //private int[] item = new int[11];

        private string[] nameslist = new string[11];
        private int index = 0; 
        public string this[int index]
        {
            get
            {
                return nameslist[index];
            }
            set
            {
                nameslist[index] = value;
            }
        }

        public int this[string indexContent]
        {
            get
            {
                for (int i = 0; i < nameslist.Length; i++)
                {
                    if (indexContent == nameslist[i])
                    {
                        index = i;
                        break;
                    }
                }
                return index;
            }
            set
            {
                for (int i = 0; i < nameslist.Length; i++)
                {
                    if (indexContent == nameslist[i])
                    {
                        nameslist[i] = value.ToString();
                    }
                }
            }
        }
    }
    class UnderstandingIndexer
    {
        public static void Main()
        {
            IndexerExample obj = new IndexerExample();
            obj[4] = "Nagendra";
            // calls the property with return type string
            Console.WriteLine(obj[4]);

            // calls the property with return type int
            Console.WriteLine(obj["Nagendra"]);
        }
    }
}

Indexer an object to be indexed in the same way as an array. Indexer modifier can be private, public, protected or internal. The return type can be any valid C# types.Indexers in C# must have at least one parameter.

Monday, August 18, 2014

Understanding interfaces in C#

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

namespace Apps
{
    // Interface is a collection of abstract members and properties.
    // Unlike the Abstract class the interface cannot contain the non-abstract members.

    interface Iclass1
    {
        // A property in interface can be specified without any body
        int a { set; get; }
        void PrintProperty();

        // Access modifiers for the members of interface defaults to Public
        void Read();
}

    interface Iclass2
    {
        void Write();
}

    class Test9 : Iclass1Iclass2      // Multiple interfaces
{

       // The class should implement all the methods and properties contained in the interface.(if not gives compile error)
        public int a { setget; }

        public void Print()
        {
            Console.WriteLine("Non-interface method implemented in class, Value=" + a);
        }

        public void PrintProperty()
        {
            Console.WriteLine(a);
        }

        public void Read()
        {
            Console.WriteLine("Reading example");
        }

        public void Write()
        {
            Console.WriteLine("Writing example");
        }
    }

    class UnderstandingInterfaces
    {
        public static void Main()
        {
            Iclass1 ts = new Test9();
            Iclass2 ts1 = new Test9();
            ts.Read();
            ts1.Write();
            ts.PrintProperty();

            Test9 ts2 = new Test9();
            ts2.Print();
        }
    }
}

Sunday, August 17, 2014

Understanding Runtime Polymorphism

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

namespace ConsoleApps
{
    class BaseClass
    {
        public void Normal()
        {
            Console.WriteLine("Base Class print");
        }
        public virtual void RuntimePoly()
        {
            Console.WriteLine("Base Class print");
        }
    }

    class DerivedClass : BaseClass
    {
        public new void Normal()
        {
            Console.WriteLine("Derived Class print");
        }

        public override void RuntimePoly()
        {
            Console.WriteLine("Derived Class print");
        }
    }

class UnderstandingRuntimePolymorphism
{
 public static void Main()
{
           // Here Base class method is called
           BaseClass nm = new DerivedClass();
           nm.Normal();

          // Here Derived class method is called
            // Due to run time decision (late binding or runtime polymorphism)
            // since the methods are overriden with ovveride keyword in derived class
            // and virtual keyword in base class of the methods

           BaseClass poly = new DerivedClass();
           poly.RuntimePoly();
     }
 }
}