C# what i know:

 Namespace is to hold all the classes and arrange those, they are like bookshelves where we collect the books. Classes are like books here, public is an access modifier, if a book is accessible to the public or is it private. Inside a method parameter is a property.

যখন বই পড়তে জাই, তখন আমাদের খুজতে হয় বুকশেলফ এ। এই বুকশেলফ হলো  নেমস্পেস। এখানে বিভিন্ন রকমের বই রাখা হয়, একেক টা বই ক্লাস আকারে সাজানো থাকে সেখানে। বই গুলো পাব্লিক না কি প্রাইভেট হবে তার উপর নির্ভর করে কে কে বই পড়তে পারবে। সেভাবেই ক্লাস গুলো প্রাইভেট আর পাব্লিক এক্সেস মডিফাইয়ার হিসেবে রাখা হয় খুব সহজে কেউ সেগুলো নিতে চাইলে আমরা সেটা কন্ট্রল করতে পারবো। মেথড এর ভেতরে প্রপারটি রাখা হয় জাকে আমরা প্যারামিটার বলি।

Encapsulation:

Protect important data inside the class which we don't want to expose outside the class. Binds a data member and member function in a single unit. We can make variables, properties and methods private. There are six access modifiers: 

ক্লাস এর ভেতরে যে সকল তথ্য আমরা ক্লাস এর বাইরে প্রকাশ করতে চাই না তার জন্য ইনক্যাপসুলেশন ব্যাবহার করা হয়। ডাটা মেম্বার আর মেম্বার ফাংশন কে বাইন্ড করে রাখে একই ইউনিট এ, মানে ভ্যারিয়েবল আর ফাংশন এর উপর কন্ট্রল আনতে ব্যাবহার করা হয়। যদি আমি কোনো ভ্যারিয়েবল বা ফাংশন কে চাই নিজের মত করে নিয়ন্ত্রন করতে কোথায় কখন ব্যাবহার করবো, তার জন্য আমাদের ইনক্যাপসুলেশন ব্যবহার করা হয়।

Private:Default access modifier. Scope of the accessibility is only inside the classes.

শুধুমাত্র যে ক্লাস এ ব্যবহার করা হয় সে ক্লাস এই কাজ করবে প্রাইভেট।

Public: Anywhere within the class or outside. 

যে কোনো ক্লাস এ, ভেতরে বা বাইরের যে কোনো ক্লাস এ ব্যবহার করা জায়।

Protected:When we want the class and all derived classes to be able to access the methods or variables, but don't want it to be public.

যে ক্লাস এ ব্যবহার করা হয় সেখানে মানে প্যারেন্ট এ, আর তার চাইল্ড  ক্লাস এ ব্যবহার করা জায়।

Internal: Hides its member variables and methods from other classes and objects that is resides in other namespace, but the variables and classes that are declared with internal can be access by any member within the application (assembly).  It's a default access specifier in a class.

ইন্টারনাল শুধু প্যারেন্ট ক্লাস এ আর যে আসেম্বলি তে আছে সেখানে ব্যবহার করা জায়। এটা শুধু নিজের। চাইল্ড এর জন্য না।

Protected Internalঃ

প্যারেন্ট ক্লাস, চাইল্ড ক্লাস আর নিজের গোত্রে মানে এসেম্বিলি তে ব্যবহার করা জায়। গোত্রের বাইরে না।

Private Protected:We can access members inside the containing class or in a class that derived from a containing class as only in the same project, from another assembly if we try to access then we will get error.

শুধু নিজের মানে প্যারেন্ট ক্লাস এ ব্যবহার করা জায় আর তার চাইল্ড ক্লাস এ।

 


                containing-class,    derived-class,    containing-assembly,    anywhere-outside-the c-a

public         yes                        yes                        yes                            yes

(protected-  yes                         yes                        yes                            no

internal) 


protected    yes                            yes                        no                            no

private        yes                            no                          no                            no

internal        yes                            no                        yes                            no

Object: Hypothetically there is a BankAccount class and there will be account_id, account_password, bank_balance etc. When a new customer will come to create a bank then we need to make a new object for the class, those objects are instances and we use new for that.

ব্যাংক এ কারো জন্য কোনো নতুন একাউন্ট খোলা মানে হচ্ছে নতুন অবজেক্ট খোলা, নিউ দিয়ে আমরা নতুন অব্জেক্ট খুলি যেখানে মানুষটার সকল তথ্য থাকে। বানানো সকম অব্জেক্ট কে ইন্সট্যান্স বলে।

class BankAccount
{
    public int account_id { get; set; }
    public string account_password { get; set; }
    public decimal bank_balance { get; set; }
}
// when a new customer wants to create a bank account
BankAccount ba = new BankAccount();


Static:

A static class is a class which object can not be created and which can not be inherited. Use of static: Static classes are used as containers for static members like methods, constructors etc.

Without a static, if we want to use a variable in a class, we have to create an object first. Static modifier can be used with a variable, method, constructor or class. 

স্ট্যাটিক ক্লাস এর কোনো অব্জেক্ট থাকে না, এটি বাইরের আবরন হিসেবে কাজ করে, মেথড আর কন্সট্রাক্টর এর উপর। স্ট্যাটিক ছাড়া যদি ভ্যারিয়েবল ব্যবহার করতে চাই তাহলে আমাদের অবজেক্ট বানাতে হবেই, স্ট্যাটিক মডিফাইয়ার ভ্যারিয়েবল, মেথড, কন্সট্রাক্টর এর মধ্যে কাজ করে।

Fist, without static veriable we have to create an object:

class Employee

{

public string Name {get; set}

}

private void Form1_Load(object sender, EventArgs e)

{

Employee employee = new Employee();

 

Second, with static variable we don't need to create objects: 

Here, variable is static:

class Employee

{

public static string Name {get;set}

}

private void Form1_Load(object sender, EventArgs e)

{

Employee.Name = "Charles";

}

Constructor: Two types of constructor: instance and static.

Instance constructor:

যেখানে স্ট্যাটিক লেখা থাকে না সেটাই ইন্সটান্স কন্সট্রাক্টর।

It is used to initialize the instance data. Instance data is the instance variables.

class Car
{
    // Instance variables (instance data)
    public string Brand;
    public string Model;
    public int Year;

    // Constructor to initialize instance data
    public Car(string brand, string model, int year)
    {
        Brand = brand;
        Model = model;
        Year = year;
    }

    // Instance method accessing instance data
    public void DisplayDetails()
    {
        Console.WriteLine($"Brand: {Brand}, Model: {Model}, Year: {Year}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating objects of Car class with different instance data
        Car car1 = new Car("Toyota", "Camry", 2020);
        Car car2 = new Car("Honda", "Civic", 2018);

        // Accessing instance data through instance method
        car1.DisplayDetails(); // Output: Brand: Toyota, Model: Camry, Year: 2020
        car2.DisplayDetails(); // Output: Brand: Honda, Model: Civic, Year: 2018
    }
}

 Instance constructor is called everytime when a class object is created. It takes parameter it has access specifier means it can define variable as public or private.

ইন্সটান্স প্যারামিটার নিতে পারে যেখানে প্রাইভেট পাব্লিক ব্যবহার করা জায়।

Static constructor used to initialize the static data or used to perform once in the lifecycle of a class.It is the first block of code to be execute in class. Does not take any parameter.

স্ট্যাটিক তার জীবনে একবার ব্যবহার করে ক্লাস এর ভেতরে আর এটি মানে স্ট্যাটিক কন্সট্রাকটর সবার প্রথমে কাজ করে কোড এর ভেতরে। কোনো প্যারামিটার নেয় না।

Constructor has the same name as the class instance constructor can contain parameter as an arguement.

Static constructor cannot have a parameter because they are automatically invoked by the runtime and do not take any arguement. 

Static class: If we define a static class then we have to define all the elements of the class to be static, otherwise there will be an error.

Inheritance: Existance class is base class, and new class is called derived class.

School has a lot of objects like Humans, Materials. Humans are students and teachers so they are sparate object. Also teacher has regular teacher and head teacher so they are classified but they share common classes so we can inherite the class for not using anything over and over again.

class Human

{

public char Gender {get; set;}

public string name {get; set;}

}

class Employee : Human

{

public double Salary {get; set}

}

There are four types of inheritance:

Single: One base class and one derived class.

একটা প্যারেন্ট আর একটা চাইল্ড থাকে।

Hierarchical: Multiple classes derived from one base class. Ex: Human = Student + Employee  

একটা প্যারেন্ট থেকে দুইটা বা তার বেশি চাইল্ড ক্লাস হয়।

Multi-level: Ex: Human = Employee = Teacher

প্যারেন্ট থেকে চাইল্ড তারপর চাইল্ড থেকে আরেকটা চাইল্ড হয়।

Multiple: C# does not support, we can use multiple inheritances only through interfaces.  

মাল্টিপোল ইনহ্যারিটেন্স পেতে ইন্টার্ফেস ব্যবহার করা হয়।

Inheritance is good for the code visibility and for writting clean code. But many members remains unused and occupy the memory if it is not implemented properly.

Interface:

Just like a class it only do the function call nothing else means only the declaration of methods, events or properties that class or struct can implement. Means does not contain variable, does not contain constructor, it does not have access modifier but by default everything is Public. Object members can not be Static.

Its more like a contract and class do the rest by extending interface for example interface do the method declaration and set up method property of course those will be all in public and then later class will use inheritance of interface as it is working as another class.

interface Computers

{

void getLaptop();

void getDesktop();

}

class DellComputers : Computers

{

void Computer.getLaptop() {}

void Computer.getDesktop() {}

}

Interface can not make object directly but it can create object by another class or struct.

Computer computer = new DellComputers();

Ex:

interface Computers

{

void GetLaptop();

void GetDesktop();

}

interface Laptop

{

void GetName();

}

class DellLaptop : Computers, Laptop

{

void Computers.GetLaptop(){}

void Computers.GetDesktop(){}

void Laptop.GetName();

}


Ploymorphism: More than one form. Multiple forms. When the classes has same name as methods but those methods will work differently. Two types of polymorphism: 

compile time: method overloading or operator overloading also known as static binding or early binding. 

Multiple methods with a same name but different parameters. We can perform diffrent task by same method names but different parameters thats why the process is called overloading.

run time: method overriding also known as dynamic binding or late binding.

Creating virtual method in the base class and in the derived class we can override by using override keyword. Although it is optional for derived class to override the method.When the virtual method has a different definition than the derived class then we need that override keyword to simply override that. Simple thing to understand on the override, when we need to add something new then we override from the virtual method.


Enum: 

On this we define the variable and the type will be int autometically and start from zero if not defined. If we define anyone in the middle then it will increase siquentially.

Constructor: 

https://www.educba.com/constructor-in-c-sharp/

Sealed modifier:

Sealed class don't allow that class to be inherited.

class A{}

sealed class B : A{}

Here we can make inheritance form class A but we can't make inheritance from class B as it is with sealed modifier. We can use the same principle for the methods also. If we use sealed to any method we can not use that with inheriting the class otherwise we are going to take the error for that.

class X
{
    protected virtual void F() { Console.WriteLine("X.F"); }
    protected virtual void F2() { Console.WriteLine("X.F2"); }
}

class Y : X
{
    sealed protected override void F() { Console.WriteLine("Y.F"); }
    protected override void F2() { Console.WriteLine("Y.F2"); }
}

class Z : Y
{
    // Attempting to override F causes compiler error CS0239.
    // protected override void F() { Console.WriteLine("Z.F"); }

    // Overriding F2 is allowed.
    protected override void F2() { Console.WriteLine("Z.F2"); }
}


Copy constructor: 

We make two constructor and we use one constructor's instance to the other constructor and it is being coping and that's why it is called copy constructor.

public sealed class Person
{
    // Copy constructor.
    public Person(Person previousPerson)
    {
        Name = previousPerson.Name;
        Age = previousPerson.Age;
    }

    //// Alternate copy constructor calls the instance constructor.
    //public Person(Person previousPerson)
    //    : this(previousPerson.Name, previousPerson.Age)
    //{
    //}

    // Instance constructor.
    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public int Age { get; set; }

    public string Name { get; set; }

    public string Details()
    {
        return Name + " is " + Age.ToString();
    }
}

class TestPerson
{
    static void Main()
    {
        // Create a Person object by using the instance constructor.
        Person person1 = new Person("George", 40);

        // Create another Person object, copying person1.
        Person person2 = new Person(person1);

        // Change each person's age.
        person1.Age = 39;
        person2.Age = 41;

        // Change person2's name.
        person2.Name = "Charles";

        // Show details to verify that the name and age fields are distinct.
        Console.WriteLine(person1.Details());
        Console.WriteLine(person2.Details());

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
// Output:
// George is 39
// Charles is 41

Comments

Popular posts from this blog

c# .net learning From 19 Sep 2023

template

settings.json