Namespaces, classes without constructor, Static class & Composition

Step1 

Create a console application BCCI

Step2

Add a class Skill.cs with following code

namespace BCCI
{
    public class Skill
    {
        public string SkillName { get; set; }
        public int ExperienceInYears { get; set; }
    }

}


Rename the namespace as SkillsData, then it looks like below

namespace SkillsData
{
    public class Skill
    {
        public string SkillName { get; set; }
        public int ExperienceInYears { get; set; }
    }

}

Step3

Add a class Player.cs and to refer the above class Skill.cs like below

using SkillsData;

namespace BCCI
{
    public class Player
    {
        public string Name { get; set; }
        public Skill Skills { get; set; }
    }
}


Step4

Add a class Utility.cs with following code

namespace BCCI
{
    class Utility
    {
        public static string DisplayPlayerSkillDetail(Player p)
        {
           return $"Player:{p.Name} is a{p.Skills.SkillName} with                                                   {p.Skills.ExperienceInYears}                                                     years of experience";
        }
    }
}

Step5

In Program.cs write the following code

using System;

using SkillsData;

namespace BCCI
{
    class Program
    {
        static void Main(string[] args)
        {
            Player xPlayer = new Player();
            Skill xSkill = new Skill();
            xPlayer.Skills = xSkill;

            Console.WriteLine("Please enter the player name");
            xPlayer.Name = Console.ReadLine();
           
            Console.WriteLine("Please enter the skill name:");
            xSkill.SkillName = Console.ReadLine();

            Console.WriteLine("Please enter the experience in years:");
            xSkill.ExperienceInYears = int.Parse(Console.ReadLine());
           
            Console.WriteLine("Hi BCCI. Here is the player skill                                                              details:");
            Console.WriteLine(Utility.DisplayPlayerSkillDetail(xPlayer));

            Console.ReadKey();
        }
    }
}

Note:

1. Here program.cs and player.cs are under BCCI namespace. So Player.cs can be referred directly in program.cs

2. Skill.cs is lying under SkillsData namespace so we need to refer SkillsData namespace as
using SkillsData; in both program.cs and player.cs



No comments:

Post a Comment