Properties a short explanation using C#

What Are Properties?

  • Properties are methods that protect access to class members.
  • Properties are class members that provide access to elements of an object or class.
  • Protect access to the state of object.
  •  It likes fields, but they operate much like methods.
  • The get and set statements are called accessors.
  • Fields can’t be used in Interfaces so properties are the solution.

Syntax:

        private double balance;
        public double Balance
        {
            get
            {
                return balance;
            }
            set
            {
                balance = value;
            }
        }

Example:

    public class Employee
    {
        private double salary;
        private double taxRate = 0.05;
        

        public string Name { get; set; }
        public double YearOfExp { get; set; }

        public double YearlyMedicalAllowance {get; private set;}

        public Employee()
        {
            this.YearlyMedicalAllowance = 30000;
        }

        public double Salary
        {
            get { return salary; }
            set {
                if (value > 200000)
                    salary = value - value * taxRate;
                else
                    salary = 5000;
            }
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Employee objEmployee = new Employee();
            objEmployee.Name = "Rafiqul Islam";
            objEmployee.YearOfExp = 7;
            objEmployee.Salary = 5000;
            
            Console.WriteLine(objEmployee.Name);
            Console.WriteLine("Salary: " + objEmployee.Salary);
            Console.WriteLine("Yearly Madical Allowance" + objEmployee.YearlyMedicalAllowance);

            Console.ReadLine();
        }
    }

Static Constructor and C# implementation

Static Constructor

  • Instance constructors are used to initialize an object
  • Static constructors are used to initialize a class
  • Will only ever be executed once
  • Run before the first object of that type is created.
  • Have no parameter
  • Do not take an access modifier
  • May co-exist with a class constructor

Syntax:

        class Lion
        {
            static Lion()
            {
                // class-specific initialization
            }
        }

Example:


class RandomNumberGenerator
{
private static Random randomNumber;
public static string AuthorName { get; set; }

public RandomNumberGenerator(String msg)
{
Console.WriteLine(msg);
//Constructor for object
}

//Static constructor
static RandomNumberGenerator()
{
AuthorName = “Mahedee Hasan”;
randomNumber = new Random();
}

public int Next()
{
return randomNumber.Next();
}
}

class Program
{
static void Main(string[] args)
{
RandomNumberGenerator randomNumber
= new RandomNumberGenerator(“Generate 10 Random Number”);

for (int i = 0; i < 10; i++) { Console.WriteLine(randomNumber.Next()); } Console.WriteLine(“Author Name: ” + RandomNumberGenerator.AuthorName); Console.ReadKey(); } } [/csharp]

Love Calculator using C#

Love Calculator

    public class LoveCalculator
    {
        public string GetResults(string name1, string name2)
        {
            return GetCount(name1, name2);
        }
        private string GetCount(string firstName, string secondName)
        {
            try
            {
                string first = firstName.ToUpper();
                int firstlength = firstName.Length;
                string second = secondName.ToUpper();
                int secondlength = secondName.Length;
                int LoveCount = 0;

                for (int Count = 0; Count < firstlength; Count++)
                {
                    string singleLetter = first.Substring(Count, 1);
                    if (singleLetter.Equals("A")) LoveCount += 2;
                    if (singleLetter.Equals("E")) LoveCount += 2;
                    if (singleLetter.Equals("I")) LoveCount += 2;
                    if (singleLetter.Equals("O")) LoveCount += 2;
                    if (singleLetter.Equals("U")) LoveCount += 3;
                    if (singleLetter.Equals("A")) LoveCount += 1;
                    if (singleLetter.Equals("E")) LoveCount += 3;
                }
                for (int Count = 0; Count < secondlength; Count++)
                {
                    string singleLetter = second.Substring(Count, 1);
                    if (singleLetter.Equals("A")) LoveCount += 2;
                    if (singleLetter.Equals("E")) LoveCount += 2;
                    if (singleLetter.Equals("I")) LoveCount += 2;
                    if (singleLetter.Equals("O")) LoveCount += 2;
                    if (singleLetter.Equals("U")) LoveCount += 3;
                    if (singleLetter.Equals("A")) LoveCount += 1;
                    if (singleLetter.Equals("E")) LoveCount += 3;
                }
                int amount = 0;
                if (LoveCount > 0) amount = 5 - ((firstlength + secondlength) / 2);
                if (LoveCount > 2) amount = 10 - ((firstlength + secondlength) / 2);
                if (LoveCount > 4) amount = 20 - ((firstlength + secondlength) / 2);
                if (LoveCount > 6) amount = 30 - ((firstlength + secondlength) / 2);
                if (LoveCount > 8) amount = 40 - ((firstlength + secondlength) / 2);
                if (LoveCount > 10) amount = 50 - ((firstlength + secondlength) / 2);
                if (LoveCount > 12) amount = 60 - ((firstlength + secondlength) / 2);
                if (LoveCount > 14) amount = 70 - ((firstlength + secondlength) / 2);
                if (LoveCount > 16) amount = 80 - ((firstlength + secondlength) / 2);
                if (LoveCount > 18) amount = 90 - ((firstlength + secondlength) / 2);
                if (LoveCount > 20) amount = 100 - ((firstlength + secondlength) / 2);
                if (LoveCount > 22) amount = 110 - ((firstlength + secondlength) / 2);
                if (firstlength == 0 || secondlength == 0) amount = 0;
                if (amount < 0) amount = 0;
                if (amount > 99) amount = 99;
                return amount.ToString();
            }
            catch (Exception ex)
            {
                return "There is an error!!" + ex.ToString();
            }
        }
    }


    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Love Calculator");

            while (true)
            {
                Console.Write("Enter First Name:");

                string name1 = Console.ReadLine();
                Console.Write("Enter Second Name:");

                string name2 = Console.ReadLine();

                LoveCalculator loveCalculator = new LoveCalculator();
                string result = loveCalculator.GetResults(name1, name2);
                Console.WriteLine("Love Result :" + result + "%");
                Console.WriteLine("Press q to exit");
                string quit = Console.ReadLine();
                if (quit.ToLower().Trim() == "q")
                    break;
            }
        }
    }