Mixing class (static) methods and instance (nonstatic) methods can cause problems. - CSharp Custom Type

CSharp examples for Custom Type:static

Description

Mixing class (static) methods and instance (nonstatic) methods can cause problems.

Demo Code

using System;// www.j a v  a  2  s.c  o  m
public class Student
{
   public string _firstName;
   public string _lastName;
   public void InitStudent(string firstName, string lastName)
   {
      this._firstName = firstName;
      this._lastName = lastName;
   }
   public static void OutputBanner()
   {
      Console.WriteLine("Aren't we clever:");
   }
   public void OutputBannerAndName()
   {
      OutputBanner();
      OutputName(this);
   }
   public static void OutputName(Student student)
   {
      Console.WriteLine("Student's name is {0}", student.ToNameString());
   }
   public string ToNameString()
   {
      return _firstName + " " + _lastName;
   }
}
public class Program
{
   public static void Main(string[] args)
   {
      Student student = new Student();
      student.InitStudent("M", "C");
      Student.OutputBanner();
      Student.OutputName(student);
      student.OutputBannerAndName();
   }
}

Result


Related Tutorials