This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 7 years ago.
Newbie Incoming, coming with a question about Unity, and C# in particular. This could be really simple, but I could not figure it out.
What's the difference between public and private mean? I don't get it. public, private, it boggles my mind. I just can't get it through my head, which doesn't happen very often. Can someone explain it to me like I'm five? It would really help me out on my journey of making a ball move across the ground. Thank you in advance.
Good night .
The difference is :
Private : Those variables or " functions" which may only be used in the source class .
Public : Those variables or "functions" that can be used in various class .
Classes: A class is a construct that allows create your own custom types by grouping variables of other types , methods and events. A class is like a blueprint . Defines the data and behavior of a type.
Related
This question already has answers here:
Why should I use SerializeField?
(2 answers)
Closed 2 years ago.
I have a little question, if someone can help me, I'll be grateful.
What is Serializable field in Unity ?
How it works for an object of a class ?
In code it's looks like :
[Serializable]
or
[System.Serializable]
[Serializable]
private GameObject gameobject;
[System.Serializable]
private int timer;
The serializable field works like a public variable except that you can using it for a private variable. Some people just use a public variable but a serializable private variable works the same.
Might be a lazy answer... but Unity provides a very useful documentation (with examples) which explains every command, even [Serializable].
Here is the link to what you are looking for:
https://docs.unity3d.com/ScriptReference/Serializable.html
This question already has answers here:
Multiple Inheritance in C#
(13 answers)
Closed 4 years ago.
All similar questions I've looked though here, mentions multiple interface inheritance. However, I am not sure how MII could be a workaround to the problem.
Lets say, I have two library class (My_Class_1 and My_Class_2) of different methods. Then I want to create a new class, that can use both of these classes' methods natively, like:
public class My_Application : My_Class_1, My_Class_2 {
public My_Application(){
method_from_Class1();
smth_property_declared_in_My_Class_2 = "hello";
}
}
However, that is not possible with C#. What are flexible workarounds, to extend/enrich class with other classes? In PHP, that is unbelievably simple, just in the top of the class we can:
use example_trait_1;
use example_trait_2;
I would rather not use interfaces; in my view, they have no relation to solving this problem.
p.s. I don't want to create initialize objects for those classes. I want them to be native part of the application class.
As others mentioned, it isn't possible (because that's not what inheritance is), but it seems one workaround would be to have a public property of type Class1, inside a class that inherits Class2.
That also gets around the problem of "which class am I looking at now?" since you'd need to explicitly mention the property when you want something of type Class1.
And if you need to modify Class1 first, then you just create a separate class that inherits Class1 first, then have the public property be of that new type.
This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static itself
This question already has answers here:
labeling a group of members as private/public in c#
(3 answers)
Closed 9 years ago.
Is there a way in C# to declare a group of variables and methods as private or public like in C++ (example below). I am just trying to avoid typing a million "public"s and "private"s.
class Foo
{
private:
int Alpha;
string Dog;
public:
bool Bites;
bool Bad;
}
I keep getting an error in C# and have exhausted my internet search abilities. Thanks
No. You need to specify visibility for each member.
private is default for members, so it is safe to omit it (unless your coding guidelines tell you must specify). More details/links - Default visibility for C# classes and members (fields, methods, etc)?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Should I document my private methods?
Is it good practice write comment for private fields and private methods? I've write a simple library and I don't really know if add comment for my private fields or not.
If you have something relevant to say, it is a great idea to document it even for private members, as a guideline for future maintenance of the class. But boilerplate comments like "Gets or sets the Foo property" is pure noise for private code IMO.