Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is the difference between readonly and constant in c#

Answer Posted / anver sadat

A const must be initialized at the time of its creation. A
readonly field can be assigned to once in the class
constructor allowing you to pass in the value at run-time.
Declaring fields as const protects both you and other
programmers from accidentally changing the value of the
field. Also note that with const fields, the compiler
performs some optimization by not declaring any stack space
for the field. The readonly keyword is similar to const,
with two exceptions. First, the storage of a readonly field
is the same as a regular read-write field, and thus there is
no performance benefit. Secondly, readonly fields can be
initialized in the constructor of the containing class.

Use of the 'static' keyword, however, associates a field
with a class itself, so there will only ever be one such
field per class, regardless of the number of the class
instances (and the static field will exist even if there are
no class instances). A static field need not be constant,
however; it can be changed by code.

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Distinguish between system.string and system.text.stringbuilder classes?

877


Classes and structs can be declared as static, is this statement true or false?

978


Define method overloading in c#?

925


What is anonymous methods in c#?

1005


Can an interface extend a class c#?

894


List the fundamental oop concepts?

842


How do I create a multilanguage, single-file assembly?

872


Which are the loop types available in c#?

892


How will you allow a class to be inherited, but prevent the method from being over-ridden?

930


Is datetime value type c#?

820


How do you serialize in c#?

809


Why should I use interface in c#?

928


can you create a function in c# which can accept varying number of arguments

1021


What is the diff between System.String and System.Text.StringBuilder classes?

951


What is the difference between proc. Sent by val and by sub?

911