Created
March 17, 2015 06:28
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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