Sunday, 23 February 2020

C# Session 8: Classes in C#

Classes in C#


* A class is a Prototype or Blueprint from which an object gets generated like for example Organization is a class and HR, Finance etc… can be the object of that class.
* In other term, it is a combination of fields, methods, properties, constructors etc… which combines into an unit.

Declaration of CLASS
* A class can be defined with keyword “class” followed by a identifier or name. But there also we have some optional attributes that can be decorated while it’s declaration in following order.

Modifier: A class can have any access specifier out of public, protected, private, internal etc… In C# the default access specifier to class is internal.

Keyword: Like we are declaring with the keyword class.Identifier: The class represent by this, generally the class name. by convention it starts with a alphabet letter and in capital form. 
Base / Super Class: The parent class name if any followed by colon (:), but its optional. 
Body: The section which holds the code piece in a pair of curly braces {}.
* Constructor is responsible to initialize objects of a class.
Program We Discussed:
Note: While declaring class members like fields & all, need to take care of casing types.


Department.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp.Tutorial
{
    public class Department
    {
        // Fields
        public string deptId;
        public string deptName;

        // Properties
        public int DeptSize { get; set; }

        // Constructors
        public Department()
        {
            Console.WriteLine("Default Constructor Been Called.");
        }
    }
}


Program.cs:



using System;

namespace CSharp.Tutorial
{
    public class Program
    {
        static void Main(string[] args)
        {
            Department financeDept = new Department();
            Department hrDept = new Department();

            financeDept.deptId = "FIN001";
            financeDept.deptName = "Finance";
            financeDept.DeptSize = 60;

            hrDept.deptId = "HR002";
            hrDept.deptName = "Human Resource";
            hrDept.DeptSize = 30;

            Console.WriteLine($"Finance Dept Id: {financeDept.deptId}");
            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Angular 8 Session 21 - ngFor Directive In Angular

ngFor Directive Content will be resume soon. Stay tuned!