Better understanding of C# Properties? | Sololearn: Learn to code for FREE!
Nowy kurs! Każdy programista powinien nauczyć się Generative AI!
Wypróbuj darmową lekcję
0

Better understanding of C# Properties?

Hey guys I just finished c# classes and objects however I am still having trouble with what properties exactly are and what get does(it reads the value) but like what does that mean? dont explain set as I 100% get that. its just that like get is confusing me a bit and I want a general explanation of what properties are.

17th Nov 2017, 9:49 PM
Imaginize
Imaginize - avatar
6 odpowiedzi
0
So you don't understand objects. Objects in c# are a variable compound by variables and functions, generated by a class. An object is made out of a class by a constructor function. Object's inner variables are called attributes or properties, but in c# it sometimes also refers to different overloaded constructors and destructors. Other than constructors and destructors, the typical functions include "getters" and "setters", and they are used to access attributes that are locked by visibility (private for example). Setters are functions that set a value on one (sometimes more) of the object's attributes. Getters are to read into your program the values from one attribute of one object.
17th Nov 2017, 11:58 PM
myenemy
0
"Getters are to read into your program the values from one attribute of one object" sorry I still dont get this, i get the properties now though.
18th Nov 2017, 2:49 AM
Imaginize
Imaginize - avatar
0
Private attributes are only accesible by objects made of the class, so if you place your Main outside your class, these values can't be accesed. Getters are public or protected functions to be able to read them outside the object, like in your Main or in other functions.
18th Nov 2017, 10:51 AM
myenemy
0
ok so this for example: class Person { private string name; public string Name { get { return name; } set { name = value; } } } static void Main(string[] args) { Person p = new Person(); p.Name = "Bob"; Console.WriteLine(p.Name); } what is get doing here? Set is what allows me to write to the attribute or otherwise edit it. sorry for all the questions I just dont wanna use something I dont understand.
18th Nov 2017, 10:55 PM
Imaginize
Imaginize - avatar
0
If you don't place the get, the Console.WriteLine(p.Name); line won't compile if Main is not in the same class or you change the variable to public.
19th Nov 2017, 3:21 AM
myenemy
0
Get is what gives you the value assigned to the variables inside an object
19th Nov 2017, 3:22 AM
myenemy