Put methods that operate on a class's data inside the class. - CSharp Custom Type

CSharp examples for Custom Type:Field

Description

Put methods that operate on a class's data inside the class.

Demo Code

using System;//from   www . j  ava2 s .c o  m
public class Student
{
   public string name;
   public static void OutputName(Student student)
   {
      Console.WriteLine("Student's name is {0}", student.name);
   }
   public static void SetName(Student student, string name)
   {
      student.name = name;
   }
}
public class Program
{
   public static void Main(string[] args)
   {
      Student student = new Student();
      student.name = "Madeleine";
      Student.OutputName(student); // Method now belongs to Student.
      Student.SetName(student, "new");
      Student.OutputName(student);
   }
}

Result


Related Tutorials