C# .Net OOP How To Code
C# .Net OOP How To Code
net OOP
KST
We are provider of quality IT solution & reliable service in agile software
development
With the ever-increasing pace of globalisation and business connectivity, the demand
for IT professional is greater than ever. We work closely with our clients to create
software solution that helps small and large businesses alike to achieve their goals.
What is OOP
OOP (Object Oriented Programming) is a programming paradigm based on the concept of
objects, which can contain data and code: data in the form of fields (often known as
attributes or properties), and code, in the form of procedures (often known as
methods). Programming language support OOP: C#, Java, etc OOP language allows to break
the program into the bit-sized problems that can be solved easily (one object at a
time). The new technology promises greater programmer productivity, better quality of
software and lesser maintenance cost. OOP systems can be easily upgraded from small to
large systems.
In this explanation we will use C# dotnet, you can install dotnet core or visual
studio 2022 community. https://visualstudio.microsoft.com/vs/community/
What will we discuss
Classes
Constructor
Properties
Events
Static
Members
Interfaces
Inheritance
Polymorphism
Classes
Classes model real-world objects and define:
state
behavior
fields
methods
operations
Classes describe sturture of objects
In C# classe have:
Fields
Contants
Methods
Properties
Indexers
Events
Operators
Constrtuctors
Destructors
Interfaces
Inner classes
etc
// Constructor
public Cat(string name, string owner)
{
this.name = name;
this.owner = owner;
}
// Property
public string Name
{
get { return name; }
set { name = value; }
}
// Method
public void Meong()
{
Console.WriteLine("Meooongg!!!");
}
} // End of class definition
Access Modifier
public - accessible from any class
protected - accessible from the class itself and all its descendent classes
private - accessible from the class itself only
internal - accessible from current assembly (used by default)
Methods
cat1.Meong(); // Meooongg!!!
Constructor
Constructors are special methods
Default Constructor*
public Point()
{
x = 0;
y = 0;
}
With Parameter
public Person()
{
name = "NO NAME";
age = 0;
}
Inline initialization
public Time(){}
// Other code
}
Chaining
class Person
{
private string firstName;
private string lastName;
private int age = 2;
private string[] hobbies;
}
Read-Only
Write-Only
Read and Write
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}
Automatic Properties Properties could be defined without and underlying field behind
them
class Uer
{
public int Id { get; set; }
}
Static Members
Static can be used for
Fields
Properties
Methods
Events
Constructors
Static Non-Static
Initialzed just befor type is used for the Initialized when the constructor
first time is called
// example:
Rectangle.GetArea(2,5);
Structures
Structures represents a combination of fields with data
However classes are reference type and are placed in dynamic memory (heap)
struct Color
{
public byte red;
public byte green;
public byte blue;
}
struct Square
{
public Point location;
public int size;
public Color borderColor;
public Color surfaceColor;
}
Use structures
To make your type behave as a primitive type
If you create many instances and after that you free them - e.g. in a
cycle
Delegates
Abstraction
Encapsulation
Inheritance
Polymorphism