Friday, 29 September 2017

C# SAMPLE EXAMPLE

C# example class, enum, properties, private methods, public methods, salary calculation example..


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee tony = new Employee();
            tony.Income = 150000;
            tony.YearsOfService = 2;
            tony.SetRating(Employee.Rating.poor);
            tony.CalculateRaise();
        }
    }

    public class Employee   // new class employee
    {
        public enum Rating   // constant
        {
            poor,
            good,
            excellent
        }

        private Rating rating;  // private member variable type rating

         
// public properties member varibale income type income, a class can have properties
 public double Income { get; set; }


// public properties  memeber variable type yearofservice

        public int YearsOfService { get; set;} 

 // method setrating within a class. we have private variable rating & parameter rating
        public void SetRating(Rating rating) 

        {
            this.rating = rating;
        }

        public void CalculateRaise()
        {
            double baseRaise = Income * .05;
            double bonus = YearsOfService * 1000;
            Income = Income + baseRaise + bonus;

            switch(rating)
            {
                case Rating.poor:
                    Income -= YearsOfService * 2000;
                    break;
                case Rating.good:
                    break;
                case Rating.excellent:
                    Income += YearsOfService * 500;
                    break;
            }
            Console.WriteLine($"New income is {Income}");
        }
    }


}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.