Friday, 7 September 2018

Parent-Child class declaration and initialization

using System;

namespace CSharpDemo
{
    public class A
    {
        public void print()
        {
            Console.Write("A \n");            
        }

        // private method decalaration
        private void securePrint(){
            Console.Write("secure print A \n");
        }

        public void allowPublicAccessToSecurePrint(){
            this.securePrint();
        }
    }

    public class B : A
    {
        public void print()
        {
            Console.Write("B \n");
        }

        // base keyword will allow to access parent class's public methods
        public void printFromA(){
            base.print();
            //// parent's private methods are not accessible in child
            //// base.securePrint();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            A aa = new A();
            aa.print(); // -------------------------------------------- output: A
            //// private member not accessible
            //// aa.securePrint();
            aa.allowPublicAccessToSecurePrint(); //-------------------- output: secure print A

            A ab = new B();
            ab.print(); // -------------------------------------------- output: A
            ab.allowPublicAccessToSecurePrint(); //-------------------- output: secure print A

            //// below code will give you compile time error
            //// An explicit conversion exists (are you missing a cast?) 
            //// B ba = new A();
            //// ba.print();

            B bb = new B();
            bb.print(); // -------------------------------------------- output: B
            bb.printFromA(); // --------------------------------------- output: A
            bb.allowPublicAccessToSecurePrint(); //-------------------- output: secure print A
        }
    }
}

Sunday, 24 June 2018

Some basic dotnet core command for beginners

Install framework .Net Core 1.X or 2.X from Microsoft website.

go to command line interface

1) Check dotnet framework version
dotnet --version

2)create new dotnet project
dotnet new classlib | MVC | console  -o myFirstProj
where classlib, MVC, console are project type.

This command will create new project at current directory as per given argument.
using this command you can create new class library, MVC web application or console appliation.

3)Create solution and add project to that solution.
First create folder with name, which you want to give to solution.
now run command "dotnet new sln"
this command will create new solution file with same name as folder.

now create new project in that folder using command line interface.

To add that project to solution use below command
dotnet sln myFristSln.sln add myFirstProj/myFirstProj.csproj

To remove project from solution run below command
dotnet sln myFristSln.sln remove myFirstProj/myFirstProj.csproj

4) To resotre all packages/dependencies for all projects run below command
dotnet restore

5) To build solution file
dotnet build
build will restore packages first. To skip packages restore use "dotnet build --no-restore".

6) To run your project in your solution
dotnet run --project myFirstProj.csproj

To run single project only use "dotnet run" command.

7)To run test cases
dotnet test

8) To pack and publish your application for deployment run below command
dotnet publish

9) Help for any command
dotnet <command> -h

Parent-Child class declaration and initialization

using System; namespace CSharpDemo {     public class A     {         public void print()         {             Console.Wr...