Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created March 17, 2015 06:28
Show Gist options
  • Select an option

  • Save dnasca/93794e8cb009ed866a36 to your computer and use it in GitHub Desktop.

Select an option

Save dnasca/93794e8cb009ed866a36 to your computer and use it in GitHub Desktop.
This is redundant with get; and set; methods built-in, but this is a good way to see whats happening by doing it ourselves!
using System;
/* Not all programming languages use Properties
*
* Getter and Setter methods are used to encapsulate and protect fields
*
*/
public class Student
{
private int _id;
public string _name;
public int _passMark;
public void SetId(int Id)
{
if (Id <= 0)
{
throw new Exception("Student Id should be greater than zero");
}
this._id = Id;
}
public int GetId()
{
return _id;
}
}
public class Program
{
public static void Main()
{
Student S1 = new Student();
S1.SetId(1221);
Console.WriteLine("Student Id = {0}", S1.GetId());
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment