Monday, 16 July 2018

C# Collections

There are the following 2 types of collections:
  1. Non-Generic
  2. Generic

 Non-generic                          Generic

  ArrayList     ------------->          List
  HashTable  ------------->          Dictionary
  SortedList   ------------->          SortedList  
  Stack           ------------->          Stack
  Queue         ------------->          Queue

1. Non-Generic
  1. Each element can represent a value of a different type.
  2. Array Size is not fixed.
  3. Elements can be added / removed at runtime.
ArrayList
  1. Arraylist is a class that is similar to an array, but it can be used to store values of various types.
  2. An Arraylist doesn't have a specific size.
  3. Any number of elements can be stored. 
  1. using System.Collections;

  2. protected void Button1_Click(object sender, EventArgs e)  
  3. {  
  4.      ArrayList al = new ArrayList();  
  5.      string str = "kiran teja jallepalli";  
  6.      int x = 7;  
  7.      DateTime d = DateTime.Parse("8-oct-1985");  
  8.      al.Add(str);  
  9.      al.Add(x);  
  10.      al.Add(d);  
  11.       
  12.      foreach (object o in al)  
  13.      {  
  14.         Response.Write(o);  
  15.         Response.Write("<br>");  
  16.      }   
  17. }
    Output:
    kiran teja jallepalli
    7
    10/8/1985 12:00:00 AM
ForeachLoop
It executes for each and every item that exists in the arraylist object.
Every time the loop rotates it reads one item from the arraylist and assignes it to the variable.
Note:
Arraylist allocates memory for 4 items, whenever an object is created. When a fifth item is added, memory for another 4 items are added.
it reduces the memory allocated for the object.
 
Capacity: is a property that returns the number of items for which memory is allocated .
 
HashTable

HashTable is similar to arraylist but represents the items as a combination of a key and value.
  1. using System.Collections;

  2. protected void Button2_Click(object sender, EventArgs e)  
  3. {  
  4.     Hashtable ht = new Hashtable();  
  5.     ht.Add("ora""oracle");  
  6.     ht.Add("vb""vb.net");  
  7.     ht.Add("cs""cs.net");  
  8.     ht.Add("asp""asp.net");  
  9.   
  10.     foreach (DictionaryEntry d in ht)  
  11.     {  
  12.        Response.Write (d.Key + " " + d.Value);  
  13.   
  14.        Response.Write("<br>");  
  15.   
  16.     }  
  17. }
    Output:
    vb  vb.net
    asp asp.net
    cs  cs.net
    ora oracle 
DictonaryEntry: is a class whose object represents the data in a combination of key & value pairs.
SortedList: 
    1. Is a class that has the combination of arraylist and hashtable.
    2. Represents the data as a key and value pair.
    3. Arranges all the items in sorted order. 
    1. using System.Collections;

    2. protected void Button3_Click(object sender, EventArgs e)  
    3. {  
    4.      SortedList sl = new SortedList();  
    5.      sl.Add("ora""oracle");  
    6.      sl.Add("vb""vb.net");  
    7.      sl.Add("cs""cs.net");  
    8.      sl.Add("asp""asp.net");  
    9.              
    10.      foreach (DictionaryEntry d in sl)  
    11.      {  
    12.          Response.Write(d.Key + " " + d.Value);  
    13.          Response.Write("<br>");  
    14.   
    15.      }  
    16. }
      Output:
      asp  asp.net
      cs   cs.net
      ora  oracle
      vb   vb.net
    Stack:
    1. protected void Button4_Click(object sender, EventArgs e)  
    2. {  
    3.     Stack stk = new Stack();  
    4.     stk.Push("cs.net");  
    5.     stk.Push("vb.net");  
    6.     stk.Push("asp.net");  
    7.     stk.Push("sqlserver");  
    8.        
    9.     foreach (object o in stk)  
    10.     {  
    11.        Response.Write(o + "<br>");  
    12.     }  
    13. }
      Output:
      sqlserver
      asp.net
      vb.net
      cs.net  
    queue:
    1. using System.Collections;

    2. protected void Button5_Click(object sender, EventArgs e)  
    3. {  
    4.     Queue q = new Queue();  
    5.     q.Enqueue("cs.net");  
    6.     q.Enqueue("vb.net");  
    7.     q.Enqueue("asp.net");  
    8.     q.Enqueue("sqlserver");  
    9.          
    10.     foreach (object o in q)  
    11.     {  
    12.        Response.Write(o + "<br>");  
    13.     }  
    14. }  
      Output:
      cs.net
      vb.net
      asp.net
      sqlserver

    2. Generic Collections

    Generic Collections work on the specific type that is specified in the program whereas non-generic collections work on the object type. 
    1. Specific type
    2. Array Size is not fixed
    3. Elements can be added / removed at runtime.
    List:
    1. using System.Collections.Generic;  
    2.   
    3. protected void Button1_Click(object sender, EventArgs e)  
    4. {  
    5.         List<int> lst = new List<int>();  
    6.         lst.Add(100);  
    7.         lst.Add(200);  
    8.         lst.Add(300);  
    9.         lst.Add(400);  
    10.         foreach (int i in lst)  
    11.         {  
    12.             Response.Write(i+"<br>");  
    13.         }  
    14.  }  
    Dictonary: 
    1. using System.Collections.Generic;    
    2.     
    3.  protected void Button1_Click(object sender, EventArgs e)    
    4.  {    
    5.      Dictionary<intstring> dct = new Dictionary<intstring>();    
    6.      dct.Add(1, "cs.net");    
    7.      dct.Add(2, "vb.net");    
    8.      dct.Add(3, "vb.net");    
    9.      dct.Add(4, "vb.net");    
    10.      foreach (KeyValuePair<intstring> kvp in dct)    
    11.      {    
    12.          Response.Write(kvp.Key + " " + kvp.Value);    
    13.          Response.Write("<br>");    
    14.      }          
    15.   }   
    SortedList:
    1. using System.Collections.Generic; 

    2. protected void Button3_Click(object sender, EventArgs e)  
    3. {  
    4. SortedList<stringstring> sl = new SortedList<stringstring>();  
    5. sl.Add("ora""oracle");  
    6. sl.Add("vb""vb.net");  
    7. sl.Add("cs""cs.net");  
    8. sl.Add("asp""asp.net");  
    9.   
    10. foreach (KeyValuePair<stringstring> kvp in sl)  
    11. {  
    12. Response.Write(kvp.Key + " " + kvp.Value);  
    13. Response.Write("<br>");  
    14. }  
    15. }  
    Stack:
    1. using System.Collections.Generic;  

    2. protected void Button4_Click(object sender, EventArgs e)  
    3. {  
    4. Stack<string> stk = new Stack<string>();  
    5. stk.Push("cs.net");  
    6. stk.Push("vb.net");  
    7. stk.Push("asp.net");  
    8. stk.Push("sqlserver");  
    9.   
    10. foreach (string s in stk)  
    11. {  
    12. Response.Write(s + "<br>");  
    13. }  
    14. }  
    Queue:
    1. using System.Collections.Generic;
    2. protected void Button1_Click(object sender, EventArgs e)  
    3.    {  
    4.    Queue<string> q = new Queue<string>();  
    5.   
    6.    q.Enqueue("cs.net");  
    7.    q.Enqueue("vb.net");  
    8.    q.Enqueue("asp.net");  
    9.    q.Enqueue("sqlserver");  
    10.   
    11.    foreach (string s in q)  
    12.    {  
    13.    Response.Write(s + "<br>");  
    14.    }  
    15.    }  


    Difference between Array and ArrayList in C#

    The following table lists the difference between Array and ArrayList in C#.
    ArrayArrayList
    Array is strongly typed. This means that an array can store only specific type of items\elements.ArrayList can store any type of items\elements.
    Array stores fixed number of elements. Size of an Array must be specified at the time of initialization.ArrayList grows automatically and you don't need to specify size.
    No need to cast elements of an array while retriving because it is strongly type and stores specific type of items only.Items of ArrayList need to be cast to appropriate data type while retriving.
    Use static helper class Array to perform different tasks on the array.ArrayList itself includes various utility methods for various tasks.

    C# Version History

    Version .NET Framework Visual Studio Important Features
    C# 1.0 .NET Framework 1.0/1.1 Visual Studio .NET 2002 First release of C#
    C# 2.0 .NET Framework 2.0 Visual Studio 2005 Generics
          Partial types
          Anonymous methods
          Nullable types
          Iterators
          Covariance and contravariance
           
    C# 3.0 .NET Framework 3.0\3.5 Visual Studio 2008 Auto-implemented properties
          Anonymous types
          Query expressions
          Lambda expression
          Expression trees
          Extension methods
           
    C# 4.0 .NET Framework 4.0 Visual Studio 2010 Dynamic binding
          Named/optional arguments
          Generic covariant and contravariant
          Embedded interop types
           
    C# 5.0 .NET Framework 4.5 Visual Studio 2012/2013 Asynchronous members
          Caller info attributes
           
    C# 6.0 .NET Framework 4.6 Visual Studio 2013/2015 Static imports
          Exception filters
          Property initializers
          Expression bodied members
          Null propagator
          String interpolation
          nameof operator
          Dictionary initializer
           
    C# 7.0 .NET Core Visual Studio 2017 Static imports
          Exception filters
          Property initializers
          Expression bodied members
          Null propagator
          String interpolation
          nameof operator
          Dictionary initializer

    Wednesday, 11 July 2018

    IQ


    Null Coalescing Operator in C#

                                     Null-collation(??) is an important operator in C#. As per the MSDN definition: The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise, it returns the right operand. The C# Null Coalescing Operator (??)  is a binary operator that simplifies checking for null values. It is used to assign a default value to a variable when the value is null.

    Ex:
     static void Main(string[] args)
            {
                string name = "ABC";
                string myname = name ?? "EFG";
                Console.WriteLine(myname);
                Console.ReadLine();
            }

    O/P : ABC

    Ex:
    static void Main(string[] args)
            {
                string name = null;
                string myname = name ?? "EFG";
                Console.WriteLine(myname);
                Console.ReadLine();
            }

    O/P : EFG

    ORM(Object Relation Mapping):

    Entity Framework an NHiberante are ORMs. It means that you do not operate by SQL connections, commands, parameters - ORM tool does it for you and it allows to map your database structure in OOP manner: you can add, read, update, delete records in your DB using objects in C#.

    Most Used ORMs ??
    1. NHibernate: This remains my best bet and I personally feel it is the best ORM. This works well with Oracle database.
    2. Entity Framework: Microsoft’s answer to NHibernate. Works well with SQL, but still a step behind NHibernate.
    3. LINQ to SQL: A light version of ORM. Cannot be termed as an ORM in itself, but can use its features to achieve ORM’s functionality.



     static void Main(string[] args)
            {
                int num, x;
                Console.WriteLine("Enter the Number ");
                num = int.Parse(Console.ReadLine());
                Console.WriteLine("The Factors are : ");
                for (x = 1; x <= num; x++)
                {
                    if (num % x == 0)
                    {
                        Console.WriteLine(x);
                    }
                }
                Console.ReadLine();
            }
        }

        //Output--1,3,9,,27

    Remove Duplicate characters

    class Program
       {
            static void Main()
            {
                string value1 = RemoveDuplicateChars("Csharpstar");
                Console.WriteLine(value1);
            }

            static string RemoveDuplicateChars(string key)
            {
                // --- Removes duplicate chars using string concats. ---
                // Store encountered letters in this string.
                string table = "";
                // Store the result in this string.
                string result = "";

                // Loop over each character.
                foreach (char value in key)
                {
                    // See if character is in the table.
                    if (table.IndexOf(value) == -1)
                    {
                        // Append to the table and the result.
                        table += value;
                        result += value;
                    }
                }
                return result;
            }
        }
        //Input: Csharpstar

        //Output: Csharpt

    2) Calculate power of number using recursion in C#
    class Program
        {     
            static void Main(string[] args)
            {

               double x = Power(2, 3);
                Console.WriteLine(x);
                Console.ReadLine();
            }
            internal static double Power(double @base, int exponent)
            {
                if (exponent < 0)
                {
                    Console.Error.WriteLine("Usage of this function is limited to positive exponents only");
                    throw new Exception();
                }
                else if (exponent == 1)
                {
                    return @base;
                }
                else if (exponent == 0)
                {
                    return 1;
                }
                else
                {
                    return @base * Power(@base, exponent - 1);
                }
            }
        }

    O/P--8

    What Happens If You Delete a Table That Is Used by a View?
    http://dba.fyicenter.com/faq/sql_server/Deleting_a_Table_That_Is_Used_by_a_View.html

    MVC How to bundle html templates of type “text/html” or cshtml?

    can u put both js and css files in one bundle config file?
    what are the configuration are ther ein bundle config


    difference
    select sum(1) from Policy--  Output-10
    select sum(2) from Policy--  Output-20
    select count(*) from Policy-- Output-10

    How do i print the count of repeated string count?
    https://www.dotnetperls.com/duplicate-chars

    how indexes are working internally

    Wednesday, 6 June 2018

    Clearing a text box field when a radio button is selected in Javascript

     var IndexOfradiobtn = "";
            function RemovepolorTransnum() {
                var rblPivot = document.getElementById("rblPivot");
                var radiobtnList = rblPivot.getElementsByTagName("input");
                for (var i = 0; i < radiobtnList.length; i++) {
                    if (radiobtnList[i].checked && IndexOfradiobtn == i) {
                        if (radiobtnList[0].checked & txtPolnum.value != "")
                            break;
                        else if (radiobtnList[1].checked & txtPolnum.value != "")
                            break;
                    }
                    else if (radiobtnList[i].checked) {
                        txtPolnum.value = "";
                        IndexOfradiobtn = i;
                    }
                }
            }

    Strong understanding of OOP concepts, architecture, and design
    • Experience working with Microsoft’s .NET Framework
    • A strong knowledge of JavaScript and Microsoft SQL Server (including programming stored procedures and
    functions)
    • Proficiency in Microsoft Office products, including Excel, Word, and PowerPoint
    • Strong verbal and written communication skills
    • Strong technical and organization skills
    • A successful candidate should be analytical and detail-oriented, as well as team-oriented, but also be able to work
    independently