+ 3

In this program , the word "value" is not defined and where does it come from, and can another word be used instead?

using System; using System.Collections.Generic; namespace Code_Coach_Challenge {     class Program     {         static void Main(string[] args)         {             string postText = Console.ReadLine();         Post post = new Post();         post.Text = postText;             post.ShowPost();         }     }     class Post     {         private string text;              public Post (){ Console.WriteLine ("New post");              }   //write a constructor here                 public void ShowPost()         {             Console.WriteLine(text);         }         public string Text {      get {return text ;}      set { text =value;}//I am stuck at this point, please someone explain }                   } }

9th Aug 2023, 3:52 AM
safir safir1
safir safir1 - avatar
3 Answers
+ 3
safir safir1 Continue... The `value`contextual keyword that is used inside the `set` block of a property to represent the value that is being assigned to the property. For example:- If you use any other word like`tuk` instead of `value`.. You will got an error because `tuk` is not a reserved keyword in C# and is not a valid identifier. When you use `set { text = tuk; }`, the compiler does not know what `tuk` means and gives you an error. instead of `tuk`use the `value`keyword to assign the value to the `text`field see this... https://mvcwebapp.wordpress.com/2016/01/12/value-keyword-in-c/ https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value
9th Aug 2023, 4:21 AM
Darpan kesharwani🇮🇳[Inactive📚]
Darpan kesharwani🇮🇳[Inactive📚] - avatar
+ 2
The "value" is a part of the get/set accessor in a property, which works like getter and setter in other programming languages. The Text in the context is a property, so when calling it with the object we instantiated in the class "Text", it outputs the VALUE inside the text variable(of that object), if there aren't any rules applied in the property. This is because we used the keyword "value" in the set accessor. So basically, the keyword "value" actually sets the value of the given "object".. I hope I get that right. BONUS: When creating a property, if you want it to not have any specific rules on it, you can use this shortcut: public [Data Types] [Class name] {get; set;} Example: public string Name {get; set;}
9th Aug 2023, 4:10 AM
Dragon RB
Dragon RB - avatar
0
Thanks to Dragon RB and Darpan kesharwani
9th Aug 2023, 5:07 AM
safir safir1
safir safir1 - avatar