static void MyMethod()
{
Console.WriteLine("I just got executed!");
}
static void Main(string[] args)
{
MyMethod();
}
static void MyMethod(string fname,string lname)
{
Console.WriteLine(fname + " " + lname);
}
static void Main(string[] args)
{
MyMethod("ABC","PQR");
}
With method overloading, multiple methods can have the same name with different parameters
static int PlusMethod(int x, int y)
{
return x + y;
}
static double PlusMethod(double x, double y)
{
return x + y;
}
static void Main(string[] args)
{
int myNum1 = PlusMethod(8, 5);
double myNum2 = PlusMethod(4.3, 6.26);
Console.WriteLine("Int: " + myNum1);
Console.WriteLine("Double: " + myNum2);
}
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
Everything in C# is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
A Class is like an object constructor, or a "blueprint" for creating objects.
//file1.cs
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
}
//file2.cs
using System;
namespace MyApplication
{
class Car
{
public string color = "red";
}
}
Fields and methods inside classes are often referred to as "Class Members"
// The class
class MyClass
{
// Class members
string color = "red"; // field
int maxSpeed = 200; // field
public void fullThrottle() // method
{
Console.WriteLine("The car is going as fast as it can!");
}
}
A constructor is a special method that is used to initialize objects. The advantage of a constructor, is that it is called when an object of a class is created. It can be used to set initial values for fields
// Create a Car class
class Car
{
public string model; // Create a field
// Create a class constructor for the Car class
public Car()
{
model = "Mustang"; // Set the initial value for model
}
static void Main(string[] args)
{
Car Ford = new Car(); // Create an object of the Car Class (this will call the constructor)
Console.WriteLine(Ford.model); // Print the value of model
}
}
// Outputs "Mustang"
Access modifier is used to set the access level/visibility for classes, fields, methods and properties.
Modifier | Description |
---|---|
public |
The code is accessible for all classes |
private |
The code is only accessible within the same class |
protected |
The code is accessible within the same class, or in a class that is inherited from that class. |
internal |
The code is only accessible within its own assembly, but not from another assembly. |
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must
private variables can only be accessed within the same class (an outside class has no access to it). However, sometimes we need to access them - and it can be done with properties.
A property is like a combination of a variable and a method, and it has two methods: a get and a set method
//file1.cs
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
Person myObj = new Person();
myObj.Name = "Liam";
Console.WriteLine(myObj.Name);
}
}
}
//file2.cs
using System;
namespace MyApplication
{
class Person
{
private string name; // field
public string Name // property
{
get { return name; }
set { name = value; }
}
}
}