Get int type reference - CSharp Language Basics

CSharp examples for Language Basics:ref

Description

Get int type reference

Demo Code

using System;/*from  w  w  w. j a  v  a 2 s  .  c  o  m*/
using System.Collections.Generic;
using System.Text;
public enum StudentLevel
{
   HighSchool,
   Undergrad,
   Postgrad
}
public class Student : Person
{
   public StudentLevel Level { get; set; }
   public Student(string firstName, string lastName, int age, StudentLevel level) : base(firstName, lastName, age)
   {
      Level = level;
   }
}
public class Person
{
   public string FirstName { get; private set; }
   public string LastName { get; private set; }
   private int _age;
   public Person(string firstName, string lastName, int age)
   {
      FirstName = firstName;
      LastName = lastName;
      _age = age;
   }
   public ref int GetAge()
   {
      return ref _age;
   }
   public override string ToString()
   {
      return $"{FirstName} {LastName} / {_age}";
   }
}
class Program
{
   static void Main(string[] args)
   {
      Person p = new Person("John", "Smith", 23);
      int age = p.GetAge();
      ref int ageRef = ref p.GetAge();
      ageRef++;
      Console.WriteLine(p);
   }
}

Result


Related Tutorials