sarpay
11/26/2018 - 8:05 PM

Classes

There at least 3 layers built by classes.

  1. Data Access (Persistance) => Repository
  2. Business Logic / Domain => Post
  3. Presentation => Post View
  • Classes are building blocks of software applications.
  • A class encapsulates data (stored in fields) and behavior (defined by methods).
using System;
using System.Collections.Generic;

namespace Classes
{
    public class User
    {
        /* Fields (Data) */
        /*****************/

        /* auto property getter setter */
        public string Name;

        /* manual property getter setter */
        public string TitleText { 
            get { 
                if (Titles.Count > 0)
                    return $"{string.Join("/", Titles)} ";
                else return string.Empty;
            }
            set { _title = value; }
        }
        private string _title;

        /* 
         * Titles list must be initialized, otherwise it will throw a null reference exception.
         * Anytime your class contains a list, always initialize it.
         * `readonly` makes sure that the list cannot be re-initialized by a method. 
        */
        public readonly List<string> Titles = new List<string>();

        /* Constructors */
        /****************/
        /* 
         * A constructor is a method with no return type.
         * It is called when an instance of a class is created.
         * Used to initialize the fields of the class to their default values.
         * Numbers: 0, Bool: false, Ref Types (Strings, Objects): null, Chars: ''
         * As best practice, define a constructor only when an object “needs” 
         * to be initialised or it won’t be able to do its job.
        */

        public User() /* Default / Parameterless */
        {
            /* not needed, but it will compile */
            // this.Titles = new List<string>();

            this.Name = "What is your name";
        }

        /* constructor overloading */
        public User(List<string> titles) 
            : this() /* First executes the default constructor */
        {
            this.Titles = titles; /* overwrites default values */
        }

        /* constructor overloading */
        public User(User user) 
            : this(user.Titles)
        {
            this.Name = user.Name;
        }

        /* Static Methods */
        /******************/
        /*
         * can be called directly from the class without instantiation.
         * This static method creates an instance of a class with the `new` keyword.
        */
        public static User Instantiate()
        {
            /* Instance of the `User` class is now an object in memory */
            var user = new User(); /* calls the default constructor */
            return user;
        }
        public static User Instantiate(string name)
        {
            /* Instance of the `User` class is now an object in memory */
            var user = new User { /* calls the default constructor */
                Name = name /* sets the name here */
            };
            return user;
        }

        /* method overloading */
        public static User Instantiate(List<string> titles, string name)
        {
            /* calls the overloaded constructor to set the titles */
            var user = new User(titles) { 
                Name = name /* sets the name here */
            };
            return user;
        }

        public static User Instantiate(User userObj)
        {
            /* calls the overloaded constructor to set all object field values */
            var user = new User(userObj);
            
            /*
             * Since the Titles field was initialized with the `readonly` modifier,
             * it cannot be re-initialized except in a constructor.
             * BUG: this line of code will not compile: 
             * user.Titles = new List<string>();
            */

            return user;
        }

        /* Instance Method */
        /*******************/
        /* 
         * Class needs to be instantiated (turned into an object) first. 
         * Objects are classes declared in memory.
        */
        public void Introduce(string text = "")
        {
            Console.WriteLine(
                "Hi {0}{1}, {2}", 
                TitleText, Name, text);
        }
    }

    /* Static Class */
    /*****************/
    public static class Me
    {
        /* Static Fields (Data) */
        public static string Name;
        public static string LastName;

        /* Static Property */
        public static string FullName { get => $"{Name} {LastName}"; }

        /* Static Constructor */
        static Me()
        {
            Name = "Sarpay";
            LastName = "Oner";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var user5 = User.Instantiate();
            user5.Introduce();
            // => Hi What is your name,
            
            var user6 = User.Instantiate(new User{ });
            user6.Introduce();
            // => Hi What is your name,
            
            var user0 = User.Instantiate();
            user0.Introduce($"this is {Me.FullName}");
            // => Hi What is your name, this is Sarpay Oner

            var user1 = User.Instantiate(Me.FullName);
            user1.Introduce($"it's me Euclid!");
            // => Hi Sarpay Oner, it's me Euclid!

            var user2 = User.Instantiate(new List<string> {"Mr."}, "John");
            user2.Introduce($"it's {Me.FullName}");
            // => Hi Mr. John, it's Sarpay Oner

            var user3 = User.Instantiate(new List<string> {"Mrs.", "Ms."}, "Mary");
            user3.Introduce($"my name is {user2.Name}");
            // => Hi Mrs./Ms. Mary, my name is John

            var user4 = User.Instantiate(
                new User
                {
                    Titles = { "E.T." },
                    Name = "Alf"
                }
            );
            user4.Introduce($"we are {Me.FullName}, {user2.Name} and {user3.Name}");
            // => Hi E.T. Alf, we are Sarpay Oner, John and Mary
        }
    }
}