Пример #1
0
        /*
         * Marking the class field public and exposing to the external world is bad.
         * As you will not have control over what get assigned and get returned. Example id of student can't be negative.
         */
        static void Main(string[] args)
        {
            /*
             * Here we can assign any garbage value to the class fields. Which is wrong.
             */
            Student s1 = new Student();

            s1.id        = -1;
            s1.name      = null;
            s1.passMarks = 0;
            Console.WriteLine(" Id = {0}, Name = {1}, Pass marks = {2}", s1.id, s1.name, s1.passMarks);

            SchoolStudent ss = new SchoolStudent();

            // ss.SetId(-1); This will throw exception.
            // ss.SetName(null); This will throw exception.
            ss.SetId(01);
            ss.SetName("Anand Dev");
            Console.WriteLine(" Id = {0}, Name = {1}, Pass marks = {2}", ss.GetId(), ss.GetName(), ss.GetPassMarks());
        }
Пример #2
0
        /*
         * Marking the class field public and exposing to the external world is bad.
         * As you will not have control over what get assigned and get returned. Example id of student can't be negative.
         */
        static void Main(string[] args)
        {
            /*
             * Here we can assign any garbage value to the class fields. Which is wrong.
             */
            Student s1 = new Student();
            s1.id = -1;
            s1.name = null;
            s1.passMarks = 0;
            Console.WriteLine(" Id = {0}, Name = {1}, Pass marks = {2}", s1.id, s1.name, s1.passMarks);

            SchoolStudent ss = new SchoolStudent();
            // ss.SetId(-1); This will throw exception.
            // ss.SetName(null); This will throw exception.
            ss.SetId(01);
            ss.SetName("Anand Dev");
            Console.WriteLine(" Id = {0}, Name = {1}, Pass marks = {2}", ss.GetId(), ss.GetName(), ss.GetPassMarks());
        }