55 Replies
class is already public
why do we use public for every fields and methods
so you can access them from outside of the class
but is there another way?
using public ones
there are different visibility modifiers,
public
, internal
, protected
and private
i wont get into detail for all all them to keep it simple and understandable.
i will take the Human
class as base for my explanation:
an age of 65 would be fine as value. but no human should be -100 years old, right?
thats why u have mechanisms set the age only if its a valid value:
here its declared that u must be at least 0 years old (new born) or at maximum 150 years old, so everything lower than zero or higher than 150 should complain
the actual value is stored in _age
, which is has the private
visibility modifier and thus is only accessible in this very own class
the Age
property is publicly accessable, well.... to have public access to the age of a human
basically the modifiers are used to keep that human in the "correct state" at any time u interact with it.
if u could do someHuman._age = -123;
that would lead to someHuman
having an invalid state as no human should be younger than freshly born
in the case this was too complex for u, here is another human example:
for humans we want to have 2 functionalities: let them eat and let them say how much they ate yet
so 2 methods are needed Eat()
and SayHowMuchYouAte()
:
the ???
must be replaced with the actual count, which means we need to introduce a counter variable:
and to raise it when we eat:
now think about this code:
that would print
but because _eatCounter
is public
, u can modify it:
that would print
which is a lie!
the _eatCounter
should be private
, so that only from within itself it can be accessed and modified
for different circumstances different modifiers exist. public
and private
are the easiest to explain so i sticked to them for this explanation.
there is a lot more under the hood u will learn later 😉why did you use _
sorry for that wall for text
before variable names
_variableName
is a naming convention for private
members
basically the moment u see the _
in front of the name -> its a private memberthanks for everything 🙂
public int _eatCounter = 0;
would be bad as its not private, it should be private int _eatCounter = 0;
there are too much thing that i don't know
do not care too much about that yet, you are a beginner after all 😉
and this '??? '
thats just a placeholder for my explanation
oh i got it
sorry
Console.WriteLine("I ate {0} times.", _eatCounter);
<- this replaces the {0}
with the value of _eatCounter
the ??? was just to not have to explain it xD
but more importantly, do u understand the difference between public
and private
?why is _eatCounter public
thats the correct question!
short answer: it should NOT be public.
long answer:
the _eatCounter
should be private, so that only from within itself [the class] it can be accessed and modified
perfect
it's like global scope and local scope right
sort of, yes
i can't access a private variable outside of the class
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
yeah object basically has its internal state (private members) and a public interface to interact with it
in this example only one state was there
_eatCounter
. but imagine u have additional counters, like how often vegetables were eaten or how often meat.
for that u might have a method like EatASteakWithCucumbers()
where u would have to increment two of many members
(dont take my culinary knowledge into account here xD)
anyway, i think you grasped how visibility modifiers work. dont be too concerned about them for now and keep goingwhat about "static" keyword
is it complicated?
its not really complicated if u understand it xD
let me think how to explain it
(you can give ur thoughts on it meanwhile)
(ill need 1-3min for my ciggy)
sure 🙂
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
In C#, static allows you to have members (classes, methods, etc) that are not tied to any particular instance and are therefore always, globally, accessible. When applying
static
members, take the following considerations:
• If there are to be multiple instances of a class, do not use static
• If you need to track state, do not use static
• If you are going to have multi threaded workflows, do not use static unless you're aware of the caveats
static
is best used for stateless methods, extension methods/classes and in areas where you understand the pros/cons. Read more here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/staticdo u the difference between a type (class) and an instance?
(trying to figure out how to explain it)
hmm, classes for similar group of objects
but instances are more specific
is that what you were asked?
do u know anything about inheritance yet?
nope
okay, then it will be hard 😂
classes describe a certain functionality, like the
Human
from before
Human
is a type, basically like a "blue print" that describes some features (in our case the eating counter)
but its just a blue print, it doesnt describe an individual humani think i understand inheritance
it's like
there is a main class
dont get too stuck on inheritance
ok i can't explain what i understand
if u cant explain what ya understand, u dont understand it ;p (that goes for me as well!)
here is an example
okay, u only had problems phrasing it, thats okay ;p
my english is bad 😄
tbh, ill take ur code as example to explain
static
ok it's not actually mine but yeah
haha
good example tho
well the code u posted i mean xD
okk
sorry i really have a hard time rn how to explain it 😒
not a problem thanks for being helpful
it's 6:20 in the morning
and i gotta sleep
4:20am here ;p
oh
good night then
uhm, ill keep it simple for now
imagine if ya want a method that returns either a
Dog
or a Cat
instance, based on if the user input is "dog" or "cat" (both would be simple string
s)
where would u place that method?animal
😄
oh damn, i have over complicated it:
imagine you have class reflecting the age of a human:
yep
the difference betewwn
MinAge
and _value
is quite simple: MinAge
is a global thingy for Age
(the type), _value
is a (non-static) value for its own specific instance
basically static
is something directly "attached" to Age
the type, while _value
is something "attached" to an instance/object of the type Age
taken this definition of the class:
and this code:
basically static
is "global" to the type, and the other is instance based
ill hit the sack for now, but once i arise ill respond if ya wrote somethingnice example
even if i understand it 😄
thank you dude
!close
Closed!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.