Thursday, June 20, 2013

Named and optional parameter in c#

Named parameter:-

Named parameter in c# is very useful feature in .NET 4.0. It’s very useful because you need not worry about parameter sequence in function call time. Named parameter is nothing special but good to follow during program development.  You can use parameter name during call time . And in below code you can find that I have change sequence of parameter in call time. And it’s working fine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Test
    {
        public void ShowData(String Name, String Surname)
        {
            Console.WriteLine("Name:- " + Name + " Surname:-" + Surname);
        }
    }

   class Program
    {
        static void Main(string[] args)
         {
             Test t = new Test();
             t.ShowData(Surname: "Kayal", Name: "sourav");
             Console.ReadLine();
         }
    }
}



Optional parameter:-

Optional parameter in C# is very essential feature in c#.  When one parameter is optional in function then we can set default value for this parameter. Now you may think that when optional parameter is needed? Let me give one example(Yes I like it to give ! If you are regular reader then probably you know it)
Think about situation where this parameter value is sometime needed and sometimes not. For example if customer is premium then give rebate of some percentage and if not default rebate is 0%. And think about condition when user will put her particular date time value it will take otherwise default value is today’s date. Talked too much..Let’s see in action.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Test
    {
        public void ShowData(String Name, String Surname="kayal")
        {
            Console.WriteLine("Name:- " + Name + " Surname:-" + Surname);
        }
    }

   class Program
    {
        static void Main(string[] args)
         {
             Test t = new Test();
             t.ShowData(Name: "sourav");
             Console.ReadLine();
         }
    }
}

Make readonly data member without using readonly keyword.

In my previous post I have explain what is readonly modifier and how it work with. And we have learned that readonly keyword is need to use to make readonly data member. Now I am going to show you how to implement readonly field without using readonly modifier in c#.

If still now you are finding answer…. then go ahead and look below code.  The trick is very simple just declare set property as a private in your property definition for data member ,then it will not allow anyone to modify anywhere. Now you may thinking then how we will initialize value in private property? Again answer is very simple –Using constructor.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class CreateUser
    {
        public string Name
        {
            get;
            private set;
        }

        public string Surname
        {
            get;
            private set;
        }

        public CreateUser(String Name, String Surname)
        {
            this.Name = Name;
            this.Surname = Surname;
        }
    }

    class Program
    {
        static void Main(string[] args)
         {
             CreateUser objUser = new CreateUser("Sourav","Kayal");
             Console.WriteLine(objUser.Name + objUser.Surname);
             Console.ReadKey();
         }
    }
}

And if you try to assign value in this private property then it will not allow to do that.


Readonly modifier in C#

Hi friend! how are you ? I am fine and have set some goal, I will tell those later. Let’s learn readonly modifier in today’s session.

Let me explain SMDN‘s documentation in my own word. Readonly is a modifier in c# which can modify one member of class in readonly mode. And once the value is assign to readonly member it’s not possible to alter or re-initialize again.

How to set value in readonly member :-
In two fashion we can set value to readonly member.

         1) In time of declaration

When we are declaring member that time immediately we can assign value to member
For example
       public readonly Int32 Age = 25;

      2)       Within constructor of class

It’s the second option to initialize readonly member. Just assign value within constructor of class. Like,

public CreateUser()
        {
            CurrentTime = DateTime.Now;
        }



Now you may think. When to use readonly?
Think such situation, When user will register in your website automatically you want to set registration date and time for particular user and you don’t want to alter it any more. Then simply define your register date member as readonly modifier. And any code cannot able to alter your data field value. Let’s see below code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class CreateUser
    {
        public readonly DateTime CurrentTime;
        public readonly Int32 Age = 25;
        public string UserName{get;set;}

        public CreateUser()
        {
            CurrentTime = DateTime.Now;
        }
    }

    class Program
    {
        static void Main(string[] args)
         {
             CreateUser obj = new CreateUser();
            
             obj.UserName = "Sourav Kayal";
             Console.WriteLine("Name:- " + obj.UserName);
             Console.WriteLine("Age:- " + obj.Age);
             Console.WriteLine("Registration Time:- " + obj.CurrentTime);
             Console.ReadKey();
         }
    }
}


Noe if you try to try to assign value or modify value in readonly field then it won't allow to do that. Here for your clarification i am trying to assign value in readonly data member and  my compilar it not allowing me.


Wednesday, June 19, 2013

Various way to set data property of class

There are various ways to set data to class data member . Few of them I have demonstrated here.
Using Property.
Using get and set property you can set value to your data member of class. Here is the example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Person
    {
        private string Name;
        private string Surname;

        public string P_Name
        {
            get { return Name; }
            set { Name = value; }
        }
        public string p_Surname
        {
            get { return Surname; }
            set { Surname = value; }
        }
    }


    class Program
    {
        static void Main(string[] args)
         {
             Person p = new Person { P_Name = "Sourav", p_Surname = "Kayal" };
             Console.Write("Name:- "+ p.P_Name + " Surname:- " + p.p_Surname);
             Console.ReadKey();
         }
    }

}





Another way to set data to data member of class is during object creation of that particular class. We can use parameterized constructor to initialize data member. This example shows how to do it.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    public class Person
    {
        public string Name;
        public String Surname;

        public Person(String Name, String Surname)
        {
            this.Name = Name;
            this.Surname = Surname;
        }
    }

    class Program
    {
        static void Main(string[] args)
         {
             Person p = new Person("Sourav","Kayal");
             Console.Write("Name:- " + p.Name + "Surname:- " + p.Surname);
             Console.ReadKey();
         }
    }

}


Another popular technique to set data to data member of class is using function. We can use parameterized function to initialize object of class. The code will show how to do it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    public class Person
    {
        public string Name;
        public String Surname;
        public Person SetData(String Name,String Surname)
        {
            Person tmp = new Person();
            tmp.Name = Name;
            tmp.Surname = Surname;
            return tmp;
        }
       
    }

    class Program
    {
        static void Main(string[] args)
         {
             Person p = new Person();
             p= p.SetData("sourav","kayal");
             Console.Write("Sourav:-" + p.Name + " Surname:-"+ p.Surname);
            
             Console.ReadKey();
         }
    }

}


Override ToString() method in c#

I hope all of you know what is overloading and overriding. If not please get few idea about that and then continue this post, because once you did not understand besic concept of overriding then you cannot follow it. Fine you are still reading means ,you know. Let’s go ahead.

As we know .NET class library is a huge collection of classes interface and all to provide in built mechanism to developer and basically I will say that this is the real power of .NET. As we know all class in .NET inherited from super class called “Object”. And this object class contains one method called ToString().

Which return string basically. Now question is “is it possible to override this method ?” –Answer is YES. We can .
And how ? It’s very simple to do that. Just use the override keyword in function definition like.

public override string ToString()
        {
            //return base.ToString();
            return "Hello World";
        }

Here when I will call ToString() method with the class object where it belongs to ,it will return me “Hello world” string rather than doing it’s routine work. See below code to get complete idea.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    public class TestClass
    {
        public override string ToString()
        {
            //return base.ToString();
            return "Hello World";
        }
    }

    class Program
    {
        static void Main(string[] args)
         {
             TestClass obj = new TestClass();
             Console.Write(obj.ToString());
             Console.ReadKey();
         }
    }

}




Generic method in C#

Today I will show you how to implement generic method in c#. The term generic means neutral.Hmm.. Confusing ? Ok let me give another example. To make a phone call at first you have to take your phone write?. So to take your phone is generic thing now you will decide whether you will make phone call or you will send SMS or MMS.

Same concept hold true for generic method. A generic method can take various type parameter. Here my Print method can take different parameter depending upon call. Have a look of below code.    

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;

namespace BlogProject
{
    class Person
    {
        public static void Print<T>(T variable)
        {
            Console.Write(variable.ToString());
        }
    }
    class Program
    {
        static void Main(string[] args)
         {
             Person.Print<int>(2);
             Person.Print<string>("hello");
             Console.ReadKey();
         }
    }

}