Thursday 26 September 2013

What is const in C#?

Ques:-What is const in C#?

Ans:-A constant member is defined at compile time and cannot be changed at run time. Constant are declared as field , using the const keyword and must be initialized as they are declared.  for example

Public Class MyClass
{
public const double p1=12.456;

}

P1 cannot not be changed in the application anywhere else in the code as this will cause a compiler error.

Constants must be value type(sbyte, byte ,short,unshort , int , uint , long, ulong, char, float, decimal , double or bool)


Classes or Structures are initialized at run time with the new keyword , and not at compile time.

Constant can be marked as Public, Private, Protected internal or protected internal.

constant are accessed if they were static field, although they cannot use the Static keyword.

1 comment:

  1. fffffc - YouTube | Videoodl.cc
    Watch, interact and share the best videos by Fffffc, a leading content provider in video and music, on Vimeo, Facebook, Instagram, youtube to mp3 ringtone Pinterest,

    ReplyDelete