Wednesday, 17 February 2016

Generic and Non Generic Collections in C#

What is Collections?
            Collections are nothing but a grouping of items in to a single unit. Before we starting to office, we are looking at our collection that is “Bag”. Bag is collection of items like laptop (if company provided), Lunch box, Pen, ID card, Zandu balm, Vicks inhaler.
            Now the question rises in our mind is Generic and Non Generic collections
The collection of different items in to single unit is called Non Generic collections. So we conclude that our Bag is a good example of Non Generic collection, Since it contain different items as we mentioned above.

The collection of similar items in to a single unit is called Generic
Collections. So we conclude that Footwear rack is the example for Generic collections, since it contains only chapples, shoes and slippers, I hope no one can put lunch box in to Footwear rack.

            Now we think in programming view, here generic collections are nothing but the collections of same types (like int,double,long). The generic collections are specific to the type of data we are going to store in it. If we have to clear about type of data, then generic collections is the preferable choice.
        C# is loaded with different type of collections, but here I taking a single collection for explaining, that is Stack, the following program shows how our Generic Stack is going to hold the similar type of data.
namespace GenericAndNonGeneric
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack<int> GenericCollection = new Stack<int>();
            GenericCollection.Push(1);
            GenericCollection.Push(2);
            GenericCollection.Push(3);
            GenericCollection.Push(4);
            GenericCollection.Push(5);
            Console.WriteLine("The Contents of the Generic Collection are:");
            foreach(int i in GenericCollection)
            {
                Console.WriteLine(i);
            }           
            Console.ReadLine();
        }
    }
}

            In above code, our collection “GenericCollection”  is storing only integer values, if you tries to store something else apart form int type, it result in compilation error.
            The generic collections are available in namespace called System.Collections.Generic,need to specify data type in < >.
Example for Generic Collections
Array, Dictionary, List
            Non generic collections are the collections of the different types, like storing different type data as a single unit, in the below program we see Stack collection as a non generic collection.

namespace GenericAndNonGeneric
{
    class Program
    {
        static void Main(string[] args)
        {
          
            Stack NonGenericCollection = new Stack();
            NonGenericCollection.Push(1);
            NonGenericCollection.Push("C# coding");
            object o = "123Vivek";
            NonGenericCollection.Push(o);
            NonGenericCollection.Push("#@@@@*&&&");
            NonGenericCollection.Push(55.67);
            Console.WriteLine("The Contents of the Non Generic Collection are:");
            foreach(object obj in NonGenericCollection)
            {
                Console.WriteLine(obj);
            }           
            Console.ReadLine();
        }
    }
 
}

            In the above code we are putting different type of data(int,string,object,double) in to a “NonGenericCollection
            The Non Generic collections are available in namespace called System.Collections
Example for Non Generic Collections
ArrayList, Hash Table.

Now we have to look at line of code “foreach(object obj in NonGenericCollection) in above example, in this line we are converting all our entered data into a Object type, since we need a common holding point to hold different type of data, so we using object type.
Performance Issue:
Generic collections are faster than Non Generic collections, since type casting (Boxing) is happens in case of non generic collections.

Conclusion:
Based on the requirement we have to decide the usage of collections either Generic or Non Generic.

Some of the collections like Stack and Queue are used as both Generic and Non Generic collections.

Guys please put comments, it will help me to improve.

Monday, 8 February 2016

What are the different types of results in MVC

Action Result Return type
In the following list we can see the action result type



1.   ViewResult :  This basically render a specific View for the response stream. The View result basically is a class which is inherited by the abstract class of the “ ViewResultBase”, and it is used to render a view. This “ViewResultBase” class contains the methods for finding the specific view for rendering  and also for execution the results. It also contain the properties for indentifying the view to rendering for the application.
public ViewResult Index()
{
    return
 View("ViewResult");  // here It is returning a  View
}

2.   PartialViewResult :- This class also inherited from the abstract base class “ ViewResultBase” class and  it is used to rendering a partial view. This class contains the methods from finding the partial view for rendering and also for the execution of the result. This also contains the properties of  identify the partial view to rendering the application. This is not  like action result. Basically  Partial views are not like the primary thing for display the user which is the view.
public PartialViewResult Index()
{
    return
 PartialView("PartialViewData");
}

3.   RedirectResult: - It is used to perform the HTTP redirect to a given URL from user side. This class is inherited from the Action result abstract class.
public ActionResult Index()
{
    return
 Redirect("http://www.google.com/");
public
 RedirectResult Index1()
{
    return
 Redirect("http://www.google.com/");
}

4.    RedirectToRouteResult :-  This is used to redirect  using specified route data dictionary. This class is inherited from the ActionResult class.
public ActionResult Index(int? Id)
{
    return
 new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary(new { controller = "Home", action = "List", Id = new int?() }));
}

5.   ContentResult : -  this is used to return an action plain text this is also inherited from actionResult  abstract class.
public ActionResult Index()
{
    return
 Content("Hello");
public
 ContentResult ContentResultIndex()
{
    return
 Content("Hello");
}

6.   JsonResult : -  Action method on controllers basically return the JsonResult means (JavaScript Object Notation result) which can be used in AJAX application. This class inherited from the ActionResult abstract class. Here Json is provided one argument which must be serializable. 
public JsonResult Index()
{
    return
 Json("Hello My Friend!");
}

ActionResult
Helper Method
Description
ViewResult
View
Renders a view as a web page
PartialViewResult
PartialView
Section of a view,that can be rendered inside another view
RedirectResult
Redirect
Redirect to another action method
RedirectToRouteResult
RedirectToRoute
Redirect to another action method
ContentResult
Content
Returns a user-defined content type
JsonResult
Json
Retuns a serialized JSON object
JavaScriptResult
JavaScript
Returns a script that can be executed on the client
FileResult
File
Returns a binary output to write to the response
EmptyResult
(None)
returns a null result

Wednesday, 4 November 2015

Important Validation using jquery

1)   Phone number validation using jquery

<html>
<head>
//Import jquery.Min.js
    <script type="text/javascript">
        $.fn.isPhoneNumber = function (options) {
            intRegex = /^[0-9]{1,10}$/;
            phoneNumber = this.val();
            if ((phoneNumber.length != 10) || (!intRegex.test(phoneNumber))) {
                this.val("");
                this.attr("placeholder", "Invalid Phone Number");
                return false;
            }
            return true;
        };

        function validate() {
            $("#checkPhoneNumber").isPhoneNumber(this);
            return false;
        }    </script>
</head>
<body>
    <input type="text" id="checkPhoneNumber" value="ASDFG77" />
    <input type="button" onclick="return validate()"  value="Validate"/>
</body>
</html>


2)   Postal code validation

<html>
<head>
//Import jquery.Min.js
    <script type="text/javascript">

        $.fn.isPostalCodeNumber = function (options) {
            intRegex = /^[0-9]{1,4}$/;
           postalcodeNumber = this.val();
            if ((postalcodeNumber.length != 10) || (!intRegex.test(postalcodeNumber))) {
                this.val("");
                this.attr("placeholder", "Invalid Postal Code Number");
                return false;
            }
            return true;
        };
        function validate() {
            $("#checkPostalCodeNumber").isPostalCodeNumber(this);
            return false;
        }
    </script>
</head>
<body>
    <input type="text" id="checkPostalCodeNumber" value="ASDFG77" />
    <input type="button" onclick="return validate()"  value="Validate"/>
</body>
</html>



3)   English validation

<html>
<head>
//Import jquery.Min.js
    <script type="text/javascript">
     var englishAlphabetAndWhiteSpace = /^[a-zA-Z ]*$/;
        if(!englishAlphabetAndWhiteSpace.test(this.val()))        
  {                this.val("");
                this.attr("placeholder", "Invalid English Alphabets");
                return false;
            }
            return true;
        };
        function validate() {
            $("#checkEnglishAlphabets").isEnglishAlphabets(this);
            return false;
        }
    </script>
</head>
<body>
    <input type="text" id="checkEnglishAlphabets" value="ASDFG77" />
    <input type="button" onclick="return validate()"  value="Validate"/>
</body>
</html>



4)   Get  the selected value form drop down

<html>
<head>
  //Import jquery.Min.js
</head>
<body>
    <select id="countries">
        <option value="1">India</option>
        <option value="2">China</option>
        <option value="3">Russia</option>
        <option value="4">Srilanka</option>
        <option value="5">Pakistan</option>
    </select>
    <input type="button" onclick="return getSelected()" value="Get Selected Value" />
    <script type="text/javascript">
    function getSelected() {
        alert($("#countries option:selected").text());
        alert($("#countries").val());
        return false;
    }
    </script>
</body>
</html>



C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...