c# get setter 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: c# getter setter
int _x;
public int x {
get { return _x; }
set { _x = value; }
}
x = 20;
Console.WriteLine(x);
public string y = {get; set;}
public double z = {get; private set;}
Example 3: getter setter c#
class Student
{
public int Age { get; set; }
}