I get the definite feeling that you have no idea what you are doing: you seem not to understand classes at all.
Quote:
can i say that a function residing in one class can't act in other class ?
Means nothing, nothing at all.
When you create a class, you are describing an object - the first 'O' in OOPs - and saying what it can do.
You define interfaces to that class in the form of properties and methods (and others, but we'll ignore them for the moment), and you say what other classes can "see" them via access modifiers:
private
means that only this class can see them.
public
means that any class can see them.
There are other modifiers (
internal
,
protected
,
protected internal
, and
private protected
) but for the moment, just work with these two.
If you declare a method as
private
, then you can't call it from outside the class. If you declare it as
public
, you can. Except ... unless the method is
static
you can only call it if you have an instance of the class to work with. If it is
static
then you can just call it via the class name, but it can only access
static
information within the class, never anything that is specific to an instance.
Think of it like cars: I have a car, you have a car: "my car", "your car". If I put my mobile in the glove box of "my car", you don't expect to be able to open the glove box on "your car" and find my mobile there - the glove box of a car is specific to that car and no other: it is an instance property.
"My car" is black. "Your car" is green - again, an instance property (one that is not declared as
static
) because you have to specify which car you are talking about when you ask about the colour. Asking "what colour is a car?" is a silly question - it can't be answered!
On the other hand, all cars - including yours, including mine - have the same number of wheels. So you can ask "how many wheels has a car?" and expect to get the same answer: four (because if it had two, it would be a motorbike!)
"NumberOfWheels" is a static property of a Car; "Colour" is an instance property; "GloveBox" is an instance property.
To get the number of wheels, you just ask the class:
Console.WriteLine($"A car has {Car.NumberOfWheels} wheels");
But to ask what colour it is, you need to specify the specific instance of a car:
Console.WriteLine($"My car is {myCar.Colour}");
Console.WriteLine($"Your car is {yourCar.Colour}");
foreach (Car car in universityCarParkA.Cars)
{
Console.WriteLine($"The car with registration {car.RegNo} is {car.Colour}");
}
The same thing is needed to look in the glovebox:
Container gloveBox = myCar.GloveBox;
gloveBox.Open();
glovebox.Add(myMobile);
gloveBox.Close();
So your question is meaningless:
Quote:
can i say that a function residing in one class can't act in other class ?
Because functions (which are called methods in C#) can be called from anywhere provided the access is allowed by the function definition, and the right instance is used for non-static methods.
Does that make sense?