0% found this document useful (0 votes)
47 views8 pages

C# .Net OOP How To Code

The document discusses object-oriented programming concepts in C# including classes, constructors, properties, static members, inheritance, and polymorphism. It provides code examples to demonstrate how to define classes with fields, methods, and constructors in C#.

Uploaded by

yozawiratama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

C# .Net OOP How To Code

The document discusses object-oriented programming concepts in C# including classes, constructors, properties, static members, inheritance, and polymorphism. It provides code examples to demonstrate how to define classes with fields, methods, and constructors in C#.

Uploaded by

yozawiratama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

KST - C#.

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

Access modifiers (scope)


public, private, protected, internal
and can be static

// This is begin of class definition named Cat


public class Cat : Animal // This Cat Class inherit (base) class from Animal Class
{
// Fields
private string name;
private string owner;

// Constructor
public Cat(string name, string owner)
{
this.name = name;
this.owner = owner;
}
// Property
public string Name
{
get { return name; }
set { name = value; }
}

public string Owner


{
get { return owner; }
set { owner = 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)

Class and Object


For this section we will use Cat Class we talk before. To use class as object, we can
do this: Define From Cat Class we have two objects: cat1 and cat2

Cat cat1 = new Cat();


cat1.name = "kuroneko";
cat1.owner = "Yoza";

Cat cat2 = new Cat("kuroneko", "Yoza");

Methods

cat1.Meong(); // Meooongg!!!

Read and Manipulate

Cat cat2 = new Cat("kuroneko", "Yoza");


Console.WriteLine(cat2.Name) // kuroneko
cat2.Name = "shironeko";
Console.WriteLine(cat2.Name); // shironeko

Constructor
Constructors are special methods

Constructors has the same name as the class


Have no return type

Can have parameters

Can be private, protected, internal, public

Default Constructor*

public class Point


{
private int x;
private int y;

public Point()
{
x = 0;
y = 0;
}

// other code ...


}

With Parameter

As rule constructors should initialize all own class fields

public class Person


{
private string name;
private int age;

public Person()
{
name = "NO NAME";
age = 0;
}

public Person(string name, int age)


{
this.name = name;
this.age = age;
}
}

Inline initialization

public class Time


{
private int hour = 12; // Inline initialization
private int minute = 0; // Inline initialization

public Time(){}

public Time(int hour, int minute)


{
this.hour = hour; // Invoed after the inline
this.minute = minute // initialization!
}

// Other code
}

Chaining

public class Point


{
private int x;
private int y;

public Point() : this(0,0) {} // Reuse constructor

public Point(int x, int y)


{
this.x = x;
this.y = y;
}

// other code ...


}

Fields, Constants, Readonly and Properties


Fields conatin data for class instance
Can arbitratry type
Have given scope
Can be deccalred with a specific value

class Person
{
private string firstName;
private string lastName;
private int age = 2;
private string[] hobbies;
}

Constat fields are defined like fields, but:

Defined with const


Must be initialized at their definition
Their value can not be changed at runtime

public class Math


{
public const double PI = 3.14
}
Read-Only Fields, Initialized at the definition or in the constructor, and can not be
modified, also it represent runtime constant.

public class ReadOnlyExample


{
private readonly int size;
public ReadOnlyExample(int size)
{
this.size = size; // can not be further modified
}
}

Properties can be:

Read-Only
Write-Only
Read and Write

Properties should have:

Access modifier (public, protected, etc.)


Return type
Unique name
Get and / or Set part
Can contain code processing data in specific way

public class Point


{
private int x;
private int y;

public int X
{
get { return x; }
set { x = value; }
}

public int Y
{
get { return y; }
set { y = value; }
}
}

Properties are not abligatory bound to a class field - can be calculated


dynamically:

public class Rectangle


{
private float width;
private float height;

public float Area


{
get { return width * height; }
}
}

Automatic Properties Properties could be defined without and underlying field behind
them

It is automatically created by the C# compiler

class Uer
{
public int Id { get; set; }
}

Static Members
Static can be used for

Fields
Properties
Methods
Events
Constructors

Static Non-Static

Associated with a type, not with an The opposite, assocuated with an


instance instance

Initialzed just befor type is used for the Initialized when the constructor
first time is called

public class Rectangle


{
public static float GetArea(float width, float height)
{
return width * height;
}
}

// example:
Rectangle.GetArea(2,5);

Structures
Structures represents a combination of fields with data

Look like the classes but are value types


Their content is stored in the stack
Transmitted by value
Destroyed when go out of scope

However classes are reference type and are placed in dynamic memory (heap)

Their creation and destruction is slower


struct Point
{
public int X, Y;
}

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;
}

When to Use Structures?

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

Do not use structures


When you often transmit your instances as method parameters
If you use collections without generics (too much boxing/unboxing)

Delegates

Abstraction

Encapsulation

Inheritance

Polymorphism

You might also like