CSharp · Console
using System;
public class Program
{
class Person
{
public string name { get; set; }
public Person(string name)
{
// using this to distinguish between the class variable name & the parameter name
this.name = name;
}
public void GetDetails()
{
Console.WriteLine("Name: {0}", name);
// using this to pass the current instance of this class to another class method
Console.WriteLine("Favorite Number: {0}", Stuff.MakeNumberFromName(this));
}
}
class Stuff
{
// This method takes the name from a Person object that is passed to it
// and multiplies its length by 10.
public static int MakeNumberFromName(Person p)
{
return p.name.Length * 10;
}
}
public static void Main()
{
Person Bob = new Person("Bob");
Bob.GetDetails();
// outputs:
// Name: Bob
// Favorite Number: 30
}
}