Search This Blog

Thursday, November 6, 2014

Difference between const and readonly ,Static


const in c#


  • You have to initialize const variables while declaration.
  • You cannot reassign a const variable.
  • Compiler evaluates the const variables.
  • The static modifier is not allowed in a constant declaration.
  • A const field of a reference type other than string can only be initialized with null.


Example for const in c#.net



using System;
using System.Text;

namespace ProgramCAll
{

    class ProgramCall
    {
        /*A const field of a reference type other than string
        can only be initialized with null*/
        private const StringBuilder myName = null;

        public static void Main()
        {
            //You have to initilize Const varabiles while declartion
            const int myEmpId = 173524;

            //Valid scenario
            const int myNumber = 10 + 16;

            const string myName = "John";

            // Reassigning a const variable is not allowed.
            //Comment out below code to run the program
            myEmpId = 23456;

            Console.WriteLine(myEmpId);

            Console.Read();
        }
    }
}


readonly in c#

  • readonly fields can be initialized only while declaration or in the constructor.
  • Once  you initialize a readonly field, you cannot reassign it.
  • You can use static modifier for readonly fields
  • readonly modifier can be used with reference types
  • readonly modifier can be used only for instance or static fields, you cannot use readonly keyword for variables in the methods.


Example for readonly modifier in c#.net



using System;
using System.Text;

namespace ProgramCall
{
    class MyClass
    {
        //readonly can only be used with instance or static variables
        public readonly int mynumber = 10;
        public readonly int addnumber = 10 + 15;

        //readonly modifier can be used with static varaibles       
        public static string myEmployer = "ProgramCall.Com";

        //readonly can be used with reference types
        public readonly StringBuilder name = new StringBuilder("John");

        /*  readonly varaible can be intilized in constructor but
             cannot be declared in constructor    */
        public readonly string myName;

        //readonly fields can be initlized in constructor
        public MyClass(string name)
        {
            myName = name;

        }

        private void myMethod()
        {
            /* readonly modifier cannot be used in method level variables
           the below line throws a error. */
            //  readonly int num = 5;
        }

    }

    //main class
    class ProgramCall
    {

        public static void Main()
        {
            MyClass obj = new MyClass("Bill");

            Console.Read();
        }
    }
}

static

Use of the static modifier to declare a static member, means that the member is no longer tied to a specific object. This means that the member can be accessed without creating an instance of the class. Only one copy of static fields and events exists, andstatic methods and properties can only access static fields and static events. For example:

public class Car
{
  public static int NumberOfWheels = 4;
}
 
The static modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes. 
static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member. For example:

int i = Car.NumberOfWheels;


Difference between const and readonly


  • const fields has to be initialized while declaration only, while readonly fields can be initialized at declaration or in the constructor.
  • const variables can declared in methods ,while readonly fields cannot be declared in methods.
  • const fields cannot be used with static modifier, while readonly fields can be used with static modifier.
  • A const field is a compile-time constant, the readonly field can be used for run time constants.
Difference between Constant and Read-only
Constant
Read-Only
Value never changed.
Value can be changed in the constructor of the class.
Declared and Initialized in Compile time.
Declared and Initialized in Run time.
By default Constant is static, Cannot be declared as static.
Can be declared as Static.

Partial Methods in C#


A partial class or struct may contain a partial method.
One part of the class contains the signature of the method. An optional implementation may be defined in the same part or another part. If the implementation is not supplied, then the method and all calls to the method are removed at compile time.
The following conditions apply to partial methods:
  • Signatures in both parts of the partial type must match.
  • The method must return void.
  • No access modifiers are allowed. Partial methods are implicitly private.
  • Partial methods can have ref but not out parameters.
  • Partial methods cannot be extern, because the presence of the body determines whether they are defining or implementing.
  • Partial methods can have static and unsafe modifier
  • Partial methods can be generic.
  • You can make a delegate to a partial method that has been defined and implemented, but not to a partial method that has only been defined.

Partial methods enable class designers to provide method hooks, similar to event handlers, that developers may decide to implement or not.
The advantage for this is that Visual Studio can provide a graphical designer for part of the class while coders work on the other.

Difference between Object, Dynamic and Var in c#

Object
Dynamic
Var
Can able to store any kind of value, because object is the base class of all type in .net framework.
Can able to store any type of the variable, similar to old VB language variable.
Can able to store any type of value but it require to initialize at the time of declaration.
Compiler has little information about the type
Compiler doesn't have any information about the this type of variable.
It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
Object type can be passed as function argument and function also can return object type
Dynamic type can be passed as function argument and function also can return object type
Var type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.
Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation.
Allows to perform operation of given type once it get cast any user defined or primitive data type.
Casting is not require but you need to know the property and methods related to stored type
No need to cast because compiler has all information to perform operation.
Cause the problem at run time if the stored value is not get converted to underlying data type.
Cause problem if the wrong method or property accessed because all the information about stored value is get resolve only at run time
Doesn't cause problem because compiler has all info about stored value.
Useful when doesn't have more information about the data type.
Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.
Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.

C# var Vs Javascript var

Difference between C# var and Javascript var 


C#

var i = 10; // implicitly typed
int i = 10; //explicitly typed

//Initialization is mandatory
var a = 10;
//Allowed
a = 13;
//Not Allowed
a = "string";

By using above example we can say C# var is strongly typed, but inferred from whatever the value is being assigned.  

Javascript

   <script type="text/javascript">
        var a = "stringValue";
              a = 567;
              a= "2ndString"; // allowed
    </script>

No comments:

Post a Comment