Private and Public Variable Access Example in c#

Rajesh081725
Posted by Rajesh081725 under C# category on | Points: 40 | Views : 1411
Private Variable:

public class Variables
{
private static string name = "";

public static string Name
{
get { return name; }
set { name = value; }

}
}


class Program
{
Variable va = new Variable();
static void Main(string[] args)
{
va.name = "Stackoverflow"
}
}


Public Variable:

class PointTest
{
public int x;
public int y;
}

class MainClass4
{
static void Main()
{
PointTest p = new PointTest();
// Direct access to public members:
p.x = 10;
p.y = 15;
Console.WriteLine("x = {0}, y = {1}", p.x, p.y);
}
}
// Output: x = 10, y = 15

Comments or Responses

Posted by: T.Saravanan on: 12/11/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly post your code inside the code tag.

Login to post response