Translate

Thursday, 3 July 2014

optional and Named parameters: C#

Program that uses optional parameters: C#

using System;

class Program
{
    static void Main()
    {
// Omit the optional parameters.
Method();

// Omit second optional parameter.
Method(4);

// You can't omit the first but keep the second.
// Method("Dot");

// Classic calling syntax.
Method(4, "Dot");

// Specify one named parameter.
Method(name: "Sam");

// Specify both named parameters.
Method(value: 5, name: "Allen");
    }

    static void Method(int value = 1, string name = "Perl")
    {
Console.WriteLine("value = {0}, name = {1}", value, name);
    }
}

Output

value = 1, name = Perl
value = 4, name = Perl
value = 4, name = Dot
value = 1, name = Sam
value = 5, name = Allen

Saturday, 28 June 2014

Differences between Hashtable and Dictionary

Dictionary:

·         It returns error if we try to find a key which does not exist.
·         It is faster than a Hashtable because there is no boxing and unboxing.
·         Only public static members are thread safe.
·         Dictionary is a generic type which means we can use it with any data type.
·         static typing (and compile-time verification)

Hashtable:

·         It returns null if we try to find a key which does not exist.
·         It is slower than dictionary because it requires boxing and unboxing.
·         All the members in a Hashtable are thread safe,

·         Hashtable is not a generic type,

        DICTIONARY VS HASHTABLE in C# REAL TIME EXAMPLES

        Differences Between Hashtable and Dictionary

        1. Hashtable is threadsafe and while Dictionary is not.
        // Creates a synchronized wrapper around the Hashtable.
          Ex:Hashtable myhashtable = Hashtable.Synchronized(hash);
        The Synchronized method is thread safe for multiple readers and writers. Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time.

        2. Dictionary is types means that the values need not to boxing while Hashtable 
            values  need to be boxed or unboxed because it stored the  values and keys as 
            objects.         
        3. When you try to get the value of key which does not exists in the collection, the 
            dictionary throws an exception of 'KeyNotFoundException' while hashtable returns 
            null value.
        4. When using large collection of key value pairs hashtable would be considered more 
            efficient than dictionary.

        5. When we retrieve the record in collection the hashtable does not maintain the order 
            of entries while dictionary maintains the order of entries by which entries were added.

        6. Dictionary relies on chaining whereas Hashtable relies on rehashing.

        7.Dictionary is a strongly typed generic collection while hashtable collection takes a object datatype

     Example

        public void MethodHashTable()
        {
            Hashtable objHashTable = new Hashtable();
            objHashTable.Add(1, 100);    // int
            objHashTable.Add(2.99, 200); // float
            objHashTable.Add('A', 300);  // char
            objHashTable.Add("4", 400);  // string

            lblDisplay1.Text = objHashTable[1].ToString();
            lblDisplay2.Text = objHashTable[2.99].ToString();
            lblDisplay3.Text = objHashTable['A'].ToString();
            lblDisplay4.Text = objHashTable["4"].ToString();

            // ----------- Not Possible for HashTable ----------
            //foreach (KeyValuePair<string, int> pair in objHashTable)
            //{
            //    lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
            //}
        }


        Dictionary Example 

        public void MethodDictionary()
          {
            Dictionary<string, int> dictionary = new Dictionary<string, int>();
            dictionary.Add("cat", 2);
            dictionary.Add("dog", 1);
            dictionary.Add("llama", 0);
            dictionary.Add("iguana", -1);

            //dictionary.Add(1, -2); // Compilation Error

            foreach (KeyValuePair<string, int> pair in dictionary)
            {
                lblDisplay.Text = pair.Value + " " + lblDisplay.Text;
            }

          }






What is the difference between ArrayList and Generic List in C#?

Arraylist:
  1. It is like Array of objects.
  2. "System.Collections" is the Namespace for Arraylist. It automatically added to namespace section in aspx.cs page.
  3. We need not to specify object type arraylist going to contain.
  4. In arraylist each item is stored as object. And return value as object.
  5. It needs to box and unbox the elements.
  6. No type safety (Compile time)
List<T>:



  1. "System.Collections.Generic" is the Namespace for List. We need to add this.
  2. We need to specify object type which type of elements it contain. 
  3. Ex: List<string> StrObj=new List<string>();
  4. From the above example StrObj will only store string objects.
  5. It doesn't need.
  6. Type safety (compile time)
Example: 

namespace Example
{
   class Program

    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            list.Add("hello");
            list.Add(new Program()); // Oops! That's not meant to be there...
            list.Add(4);
            foreach (object o in list)
            {
                Console.WriteLine(o.ToString());
            }
            List<string > lstString = new List<string>();
            lstString.Add("ABCD");
            lstString.Add(new Program());   //Compiler error
            lstString.Add(4);   // Compiler Error
            foreach (object o in lstString)
            {
                Console.WriteLine(o.ToString());
            }  }

}


WIPO Patent Translation (language)

https://www3.wipo.int/patentscope/translate/translate.jsf?interfaceLanguage=en

Thursday, 26 June 2014

Why we can't create object to ABSTRACT & INTERFACE classes?

             We cannot create object for interface and abstract class. Because, it is possible that some methods are not implemented within interface or abstract class. In the case of an interface, non of the methods are implemented and an abstract class, abstract method cannot implemented.

Without a full implementation, how could we instantiate the class.

Runtime Polymorphism and Method hiding

Runtime polymorphism:

By runtime polymorphism we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.



namespace PolymorphismByManishAgrahari
{
    class Program
    {
        public class shape
        {
            public int _radius = 5;
            public virtual double getArea()
            {
                return 0;
            }
        }
        public class circle : shape
        {
            public override double getArea()
            {
                return 3.14 * _radius * _radius;
            }
        }
        public class sphere : shape
        {
            public override double getArea()
            {
                return 4 * 3.14 * _radius * _radius;
            }
        }
        static void Main(string[] args)
        {
            shape objBaseRefToDerived = new circle();  // run time polymorphism
            objBaseRefToDerived.getArea();

            shape objBaseRefToDerived1 = new sphere();  // run time polymorphism
            objBaseRefToDerived1.getArea();

            Console.ReadLine();
        }
    }
}

we want calculate area of circle, at the time we have to create object like

BaseclassName obj=derivedclassName()  

--------------------------------------------------------------------------------------------------
Method Hiding:

namespace PolymorphismByManishAgrahari
{
    class Program
    {
        public class Base
        {

            public virtual void Show()
            {
                Console.WriteLine("Show From Base Class.");
            }
        }

        public class Derived : Base
        {

            public new void Show()
            {
                Console.WriteLine("Show From Derived Class.");
            }
        }
        static void Main(string[] args)
        {
            Base objBaseRefToDerived = new Derived();
            objBaseRefToDerived.Show();//Output--> Show From Base Class.

            Console.ReadLine();
        }
    }
}

Output is:? Show From Base Class.