public অ্যাক্সেস মডিফায়ার लगे মেম্বার ভেরিয়েবল বা ফাংশন ক্লাসের বাইর থেকেও অ্যাক্সেসযোগ্য। এগুলো অবজেক্টের মাধ্যমে এক্সেস করা হয়।
উদাহরণ
using System;
namespace RectangleApplication
{
class Rectangle
{
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
এখানে length এবং width পাবলিক, তাই Main মেথড থেকে সরাসরি অ্যাসাইন করা হয়েছে।