CSharp examples for Custom Type:Property
class with auto property
using System;//from ww w . j a va 2 s . c o m class Student { public string Name { get; set; } // property private int average; // instance variable // constructor initializes Name and Average properties public Student(string studentName, int studentAverage) { Name = studentName; Average = studentAverage; // sets average instance variable } // property to get and set instance variable average public int Average { get // returns the Student's average { return average; } set // sets the Student's average { // validate that studentAverage is > 0 and <= 100; otherwise, // keep instance variable average's current value if (value > 0) { if (value <= 100) { average = value; // assign to instance variable } } } } public string LetterGrade { get { string letterGrade = string.Empty; // string.Empty is "" if (average >= 90) { letterGrade = "A"; } else if (average >= 80) { letterGrade = "B"; } else if (average >= 70) { letterGrade = "C"; } else if (average >= 60) { letterGrade = "D"; } else { letterGrade = "F"; } return letterGrade; } } } class MainClass { static void Main() { Student student1 = new Student("Mary", 93); Student student2 = new Student("Edith", 72); Console.Write($"{student1.Name}'s letter grade equivalent of "); Console.WriteLine($"{student1.Average} is {student1.LetterGrade}"); Console.Write($"{student2.Name}'s letter grade equivalent of "); Console.WriteLine($"{student2.Average} is {student2.LetterGrade}"); } }