Tuesday, October 13, 2015

How to display an icon for website on browser tab

If you want add your own favorite icon on your website browser tab follow the below steps..

Step 1:-
  • First of all you should have favicon image file.
  • The size of image should be 16×16 pixels and it should be named as favicon.ico
  • You can either create your own image using photo editor tool like Adobe Photoshop, Paint or use many free website like that are available .
  • www.favicon.cc is one such website.
Step 2:-
  • Place the favicon.ico image file in the root directory of your web application.
Step 3:-
  • Now include the following link in the head section of the page. If you have Asp.net Master Page, place it in head section of master page.


              <link rel=”shortcut icon” href=”~/favicon.ico” type=”image/x-icon” />

Sunday, October 11, 2015

What is difference between readonly, const and static in C#

readonly :-

  • You can create a read-only field in a class by declaring it as readonly. 
  • A readonly field can be given a value only by using an initializer when it is declared or by assigning it a value within a constructor. 
  • Once the value has been set, it can’t be changed outside the constructor.

Here is an example that creates a readonly field.

using System;

class MyClass
      {
            public static readonly int SIZE = 10;
       }

class DemoReadOnly
       {
             static void Main()
                    {  
                           int[] source = new int[MyClass.SIZE];  
                           int[] target = new int[MyClass.SIZE];

    // Give source some values.  
                        for(int i=0; i < MyClass.SIZE; i++)    
                        source[i] = i;
                        foreach(int i in source)    
                        Console.Write(i + " ");
                        Console.WriteLine();

   // Reverse copy source into target.  
                       for(int i = MyClass.SIZE-1, j = 0; i > 0; i--, j++)    
                       target[j] = source[i];
                       foreach(int i in target)    
                       Console.Write(i + " ");
                       Console.WriteLine();

                     //  MyClass.SIZE = 100; // Error!!! can’t change
      }
}

Here, MyClass.SIZE is initialized to 10. After that, it can be used, but not changed. To prove this, try removing the comment symbol from before the last line and then compiling the program. As you will see, an error will result.


const :-

The const modifier is used to declare fields or local variables that cannot be changed. 
These variables must be given initial values when they are declared. Thus, a const variable is essentially a constant.

Example:-
const int i = 10;

creates a const variable called "i " that has the value 10. Although a const field is similar to a readonly field, the two are not the same. A const field cannot be set within a constructor, but a readonly field can.


static :-

There will be times when you will want to define a class member that will be used independently of any object of that class. 

Normally, a class member must be accessed through an object of its class, but it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. 

When a member is declared static, it can be accessed before any objects of its class are created and without reference to any object. You can declare both methods and variables to be static. 

The most common example of a static member is Main( ), which is declared static because it must be called by the operating system when your program begins. 

Here is an example that declares a static variable and a static method:-

using System;

class StaticDemo
       {
             // A static variable.
             public static int Val = 100;

          // A static method.
          public static int ValDiv2()
                    {  
                          return Val/2;
                     }
         }
class SDemo
        {
             static void Main()
                     {
                         Console.WriteLine("Initial value of StaticDemo.Val is "  +  StaticDemo.Val);

                         StaticDemo.Val = 8;  
                         Console.WriteLine("StaticDemo.Val is " + StaticDemo.Val);                                                                  Console.WriteLine("StaticDemo.ValDiv2(): " +  StaticDemo.ValDiv2());
                      }
        }

The output is shown here:-

Initial value of StaticDemo.Val is 100 
StaticDemo.Val is 8 
StaticDemo.ValDiv2(): 4

As the output shows, a static variable is initialized before any object of its class is created. There are several restrictions that apply to static methods:


  •  A static method does not have a this reference. This is because a static method does not execute relative to any object.
  • A static method can directly call only other static methods of its class. It cannot directly call an instance method of its class. The reason is that instance methods operate on specific objects, but a static method is not called on an object. Thus, on what object would the instance method operate? 
  • A similar restriction applies to static data. A static method can directly access only other static data defined by its class. It cannot operate on an instance variable of its class because there is no object to operate on.

static fields are independent of any specific object, they are useful when you need to maintain information that is applicable to an entire class. Here is an example of such a situation. It uses a static field to maintain a count of the number of objects that are in existence.

// Use a static field to count instances.
using System;

class CountInst
       {
            static int count = 0;
       // Increment count when object is created.
           public CountInst()
                   {  
                          count++;
                   }
       // Decrement count when object is destroyed.
           ~CountInst()
                      {  
                            count--;
                      }
             public static int GetCount()
                      {  
                              return count;
                      }
          }
class CountDemo
        {
               static void Main()
                      {  
                            CountInst ob;
                            for(int i=0; i < 10; i++)
                                      {    
                                             ob = new CountInst();    
                                             Console.WriteLine("Current count: " + CountInst.GetCount());  
                                       }
                        }
         }

The output is shown here:-

Current count: 1
Current count: 2
Current count: 3
Current count: 4
Current count: 5
Current count: 6
Current count: 7
Current count: 8
Current count: 9
Current count: 10

    Each time that an object of type CountInst is created, the static field count is incremented. Each time an object is recycled, count is decremented. Thus, count always contains a count of the number of objects currently in existence. This is possible only through the use of a static field. There is no way for an instance variable to maintain the count because the count relates to the class as a whole, not to a specific instance. 

"The Yoga of Jnana (Knowledge of Nirguna Brahma) and Vijanana (Knowledge of Manifest Divinity)"