Translate

Friday, 11 July 2014

ViewData vs ViewBag vs TempData vs Session

ViewData

  1. ViewData is a dictionary object that is derived from ViewDataDictionary class.

    1. public ViewDataDictionary ViewData { get; set; }
  2. ViewData is a property of ControllerBase class.
  3. ViewData is used to pass data from controller to corresponding view.
  4. It’s life lies only during the current request.
  5. If redirection occurs then it’s value becomes null.
  6. It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

  1. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  2. Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
    1. public Object ViewBag { get; }
  3. ViewBag is a property of ControllerBase class.
  4. It’s life also lies only during the current request.
  5. If redirection occurs then it’s value becomes null.
  6. It doesn’t required typecasting for getting data.

TempData

  1. TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
    1. public TempDataDictionary TempData { get; set; }
  2. TempData is a property of ControllerBase class.
  3. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
  4. It’s life is very short and lies only till the target view is fully loaded.
  5. It’s required typecasting for getting data and check for null values to avoid error.
  6. It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article: Persisting Data with TempData

Session

  1. Session is an object that is derived from HttpSessionState class.
    1. public HttpSessionState Session { get; }
  2. Session is a property of HttpContext class.
  3. Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it never expires.
  4. Session is valid for all requests, not for a single redirect.
  5. It’s also required typecasting for getting data and check for null values to avoid error.

Thursday, 10 July 2014

How to detect browser close when browser [X] is clicked ? or Window Close Event of Browser

aspx page:

 <script type="text/javascript" language="javascript">

        window.onbeforeunload = closeit;
        function closeit() {
            var closeEvent = false;

            //alert(event.clientY);
            if (event.clientY < 0)
                closeEvent = true;

            if ((closeEvent)) {
                document.getElementById('btnClose').click();
            }
        }
    </script>


 <table>
            <tr>
                <td>
                    <asp:Button ID="btnClose" runat="server" OnClick="closebrowser"
                        Style="display: none" />
                </td>
            </tr>
        </table>

c# page:

public void closebrowser(object sender, EventArgs e)
    {

        try
        {
           // do something

        }
        catch (Exception ex)
        {
          throw ex;
        }

    }

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