set get code example
Example 1: c# using get set methods
using System;
public class SaleItem
{
public string Name
{ get; set; }
public decimal Price
{ get; set; }
}
class Program
{
static void Main(string[] args)
{
var item = new SaleItem{ Name = "Shoes", Price = 19.95m };
Console.WriteLine($"{item.Name}: sells for {item.Price:C2}");
}
}
Example 2: set get javascript
var person = {
language : "en",
get lang() { return this.language; },
set lang(lang) { this.language = lang; }
};
document.getElementById("demo").innerHTML = person.lang;
person.lang = "es";
Example 3: get set
class Thing {
private int secret;
public int Secret {
get {
Debug.Print("Somebody is accessing the secret!");
return secret;
}
set {
Debug.Print("Somebody is writing to the secret!");
secret = value;
}
}
}