C
C#2y ago
Ryan-T1412

❔ ✅ public class

.
55 Replies
Ryan-T1412
Ryan-T1412OP2y ago
class is already public why do we use public for every fields and methods
Saber
Saber2y ago
so you can access them from outside of the class
Ryan-T1412
Ryan-T1412OP2y ago
but is there another way? using public ones
cap5lut
cap5lut2y ago
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:
public class Human
{
public int Age;
}
public class Human
{
public int Age;
}
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:
public class Human
{
private int _age;
public int Age
{
get
{
return _age;
}
set
{
if (value < 0 || value > 150) throw new ArgumentException("Invalid age!")
_age = value;
}
}
}
public class Human
{
private int _age;
public int Age
{
get
{
return _age;
}
set
{
if (value < 0 || value > 150) throw new ArgumentException("Invalid age!")
_age = 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():
public class Human
{
public void Eat()
{
Console.WriteLine("Eating");
}
public void SayHowMuchYouAte()
{
Console.WriteLine("I ate ??? times.");
}
}
public class Human
{
public void Eat()
{
Console.WriteLine("Eating");
}
public void SayHowMuchYouAte()
{
Console.WriteLine("I ate ??? times.");
}
}
the ??? must be replaced with the actual count, which means we need to introduce a counter variable: and to raise it when we eat:
public class Human
{
public int _eatCounter = 0; // this is the counter

public void Eat()
{
_eatCounter = _eatCounter + 1; // we eat, so we increase the counter
Console.WriteLine("Eating");
}
public void SayHowMuchYouAte()
{
Console.WriteLine("I ate {0} times.", _eatCounter); // just trust me that it prints something like "I ate 13 times."
}
}
public class Human
{
public int _eatCounter = 0; // this is the counter

public void Eat()
{
_eatCounter = _eatCounter + 1; // we eat, so we increase the counter
Console.WriteLine("Eating");
}
public void SayHowMuchYouAte()
{
Console.WriteLine("I ate {0} times.", _eatCounter); // just trust me that it prints something like "I ate 13 times."
}
}
now think about this code:
Human someHuman = new Human();
someHuman.Eat();
someHuman.Eat();`
someHuman.SayHowMuchYouAte();
Human someHuman = new Human();
someHuman.Eat();
someHuman.Eat();`
someHuman.SayHowMuchYouAte();
that would print
Eat
Eat
I ate 2 times.
Eat
Eat
I ate 2 times.
but because _eatCounter is public, u can modify it:
Human someHuman = new Human();
someHuman.Eat();
someHuman._eatCounter = 0;
someHuman.Eat();
someHuman.SayHowMuchYouAte();
Human someHuman = new Human();
someHuman.Eat();
someHuman._eatCounter = 0;
someHuman.Eat();
someHuman.SayHowMuchYouAte();
that would print
Eat
Eat
I ate 1 times.
Eat
Eat
I ate 1 times.
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 😉
Ryan-T1412
Ryan-T1412OP2y ago
why did you use _
cap5lut
cap5lut2y ago
sorry for that wall for text
Ryan-T1412
Ryan-T1412OP2y ago
before variable names
cap5lut
cap5lut2y ago
_variableName is a naming convention for private members basically the moment u see the _ in front of the name -> its a private member
Ryan-T1412
Ryan-T1412OP2y ago
thanks for everything 🙂
cap5lut
cap5lut2y ago
public int _eatCounter = 0; would be bad as its not private, it should be private int _eatCounter = 0;
Ryan-T1412
Ryan-T1412OP2y ago
there are too much thing that i don't know
cap5lut
cap5lut2y ago
do not care too much about that yet, you are a beginner after all 😉
Ryan-T1412
Ryan-T1412OP2y ago
and this '??? '
cap5lut
cap5lut2y ago
thats just a placeholder for my explanation
Ryan-T1412
Ryan-T1412OP2y ago
oh i got it sorry
cap5lut
cap5lut2y ago
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?
Ryan-T1412
Ryan-T1412OP2y ago
why is _eatCounter public
cap5lut
cap5lut2y ago
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
Ryan-T1412
Ryan-T1412OP2y ago
perfect it's like global scope and local scope right
cap5lut
cap5lut2y ago
sort of, yes
Ryan-T1412
Ryan-T1412OP2y ago
i can't access a private variable outside of the class
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut2y ago
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 going
Ryan-T1412
Ryan-T1412OP2y ago
what about "static" keyword is it complicated?
cap5lut
cap5lut2y ago
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)
Ryan-T1412
Ryan-T1412OP2y ago
sure 🙂
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
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/static
cap5lut
cap5lut2y ago
do u the difference between a type (class) and an instance? (trying to figure out how to explain it)
Ryan-T1412
Ryan-T1412OP2y ago
hmm, classes for similar group of objects but instances are more specific is that what you were asked?
cap5lut
cap5lut2y ago
do u know anything about inheritance yet?
Ryan-T1412
Ryan-T1412OP2y ago
nope
cap5lut
cap5lut2y ago
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 human
Ryan-T1412
Ryan-T1412OP2y ago
i think i understand inheritance it's like there is a main class
cap5lut
cap5lut2y ago
dont get too stuck on inheritance
Ryan-T1412
Ryan-T1412OP2y ago
ok i can't explain what i understand
cap5lut
cap5lut2y ago
if u cant explain what ya understand, u dont understand it ;p (that goes for me as well!)
Ryan-T1412
Ryan-T1412OP2y ago
public class Animal
{
public string Name { get; set; }
public int Age { get; set; }

public virtual void MakeNoise()
{
Console.WriteLine("Some generic animal noise");
}
}

public class Dog : Animal
{
public string Breed { get; set; }

public override void MakeNoise()
{
Console.WriteLine("Woof!");
}
}

public class Cat : Animal
{
public string FurColor { get; set; }

public override void MakeNoise()
{
Console.WriteLine("Meow!");
}
}
public class Animal
{
public string Name { get; set; }
public int Age { get; set; }

public virtual void MakeNoise()
{
Console.WriteLine("Some generic animal noise");
}
}

public class Dog : Animal
{
public string Breed { get; set; }

public override void MakeNoise()
{
Console.WriteLine("Woof!");
}
}

public class Cat : Animal
{
public string FurColor { get; set; }

public override void MakeNoise()
{
Console.WriteLine("Meow!");
}
}
here is an example
cap5lut
cap5lut2y ago
okay, u only had problems phrasing it, thats okay ;p
Ryan-T1412
Ryan-T1412OP2y ago
my english is bad 😄
cap5lut
cap5lut2y ago
tbh, ill take ur code as example to explain static
Ryan-T1412
Ryan-T1412OP2y ago
ok it's not actually mine but yeah haha good example tho
cap5lut
cap5lut2y ago
well the code u posted i mean xD
Ryan-T1412
Ryan-T1412OP2y ago
okk
cap5lut
cap5lut2y ago
sorry i really have a hard time rn how to explain it 😒
Ryan-T1412
Ryan-T1412OP2y ago
not a problem thanks for being helpful it's 6:20 in the morning and i gotta sleep
cap5lut
cap5lut2y ago
4:20am here ;p
Ryan-T1412
Ryan-T1412OP2y ago
oh good night then
cap5lut
cap5lut2y ago
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 strings) where would u place that method?
Ryan-T1412
Ryan-T1412OP2y ago
animal 😄
cap5lut
cap5lut2y ago
oh damn, i have over complicated it: imagine you have class reflecting the age of a human:
public class Age
{
public static int MinAge = 0;
public static int MaxAge = 150;

// instance members
private int _value;
}
public class Age
{
public static int MinAge = 0;
public static int MaxAge = 150;

// instance members
private int _value;
}
Ryan-T1412
Ryan-T1412OP2y ago
yep
cap5lut
cap5lut2y ago
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:
public class Age
{
public static int MinAge = 0;
public int Value;
}
public class Age
{
public static int MinAge = 0;
public int Value;
}
and this code:
Console.WriteLine(Age.MinAge); // global
Console.WriteLine(Age.Value); // local; this will error because its not static

Age instance = new Age();
instance.Value = 21; // this isnt erroring because its on an instance
Console.WriteLine(instance.Value);
Console.WriteLine(Age.MinAge); // global
Console.WriteLine(Age.Value); // local; this will error because its not static

Age instance = new Age();
instance.Value = 21; // this isnt erroring because its on an instance
Console.WriteLine(instance.Value);
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 something
Ryan-T1412
Ryan-T1412OP2y ago
nice example even if i understand it 😄 thank you dude !close
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server