DIFFERENCE BETWEEN CONST AND READ ONLY

DIFFERENCE BETWEEN CONST AND READ ONLY

const, readonly

public class MyMath{

// private fields

int A = 10;

const int B=20;

readonly int C;

// constructors

public MyMath() {

C = 10;    }

public MyMath(int a) {

C = a;    }

public MyMath(int a, int b) {

C = a + b;    }

// public property

public int ValueA    {

get { return A; }

set { A = value; }    }

//public Method

public int SumABC()    {

return (A + B + C);}}

==============

int A = 10;

const int B=20;

readonly int C;

A=10, B=20, C=10

MyMath mm = new MyMath();

int d = mm.SumABC();   // 40

A=10, B=20, C=20

C=a;

MyMath mm = new MyMath(20);

int d = mm.SumABC();   // 50

A=10, B=20, C=50

C=a+b;

 

MyMath mm = new MyMath(20, 30);

int d = mm.SumABC();   // 80

MyMath mm1 = new MyMath(15, 25);

mm1.ValueA= 35; // public property, set

A=35, B=20, C=40

C=a+b

int d = mm1.SumABC(); //95

—–or———

public class MyMath

{

// Fields – Default access  modifier is “private”

int a;

const int A = 55;

readonly int B;

// Overloaded Constructors

public MyMath() { B = 10;  }

public MyMath(int a, int b) { B = 20; }

public MyMath(int a, int b, int c) { B = a + b + c; }

 

// Public methods

public string SumOfAB()    {

int c = A + B;

return c.ToString();}}

MyMath  mm1 = new MyMath();

string x=               mm1.SumOfAB();            “65”

MyMath  mm2 = new MyMath(1, 2);

string x=               mm2.SumOfAB();            “75”

MyMath  mm3 = new MyMath(22, 33, 44);

string x=               mm3.SumOfAB();            “154”

Learn ASP.NET

ASP.NET Tutorials

* DIFFERENCE BETWEEN CONST AND READ ONLY

About

Hi, This is R K Gupta, an IT professional and Entrepreneur. I have written many articles yet for my websites gsesoftsolutions.com, Hope you like it. Kindly subscribe our feed for any updates. Add me in Facebook and Google+

Posted in Microsoft .Net Help, Software Development

Leave a Reply

Your email address will not be published. Required fields are marked *

*