[C#] C# OOP & Class and Object
Categories: CS
📋 This is my note-taking from what I learned in the class “Programming 2”
What is OOP?
OOP: 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:
- OOP is faster and easier to execute.
- OOP provides a clear structure for the programs.
- OOP helps to keep the C# code DRY “Don’t Repeat Yourself”, and makes the code easier to maintain, modify and debug.
- OOP makes it possible to create full reusable applications with less code and shorter development time.
What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
Example of Class and Object:
Class | Objects |
---|---|
Fruit | Apple |
Banana | |
Mango |
- Class is a template for objects.
- Object is an instance of a class.
When the individual objects are created, they inherit all the variables and methods from the class.
Classes and Objects
- 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.
Create a Class
Create a class named “Car” with a variable color:
class Car
{
string color = "red";
}
When a variable is declared directly in a class, it is often referred to as a field (or attribute).
It is not required, but it is a good practice to start with an uppercase first letter when naming classes. Also, it is common that the name of the C# file and the class matches, as it makes our code organized. However it is not required (like in Java).
Create an Object
Create an object called “myObj” and use it to print the value of color:
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
// Car myObj -> Car is the name of variable
// new Car() -> Car is the name of Class
Multiple Objects
Create two objects of Car:
class Car
{
string color = "red";
static void Main(string[] args)
{
Car myObj1 = new Car();
Car myObj2 = new Car();
Console.WriteLine(myObj1.color);
Console.WriteLine(myObj2.color);
}
}
Using Multiple Classes
You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the fields and methods, while the other class holds the Main() method (code to be executed)).
- prog2.cs
- prog.cs
class Car
{
public string color = "red";
}
class Program
{
static void Main(string[] args)
{
Car myObj = new Car();
Console.WriteLine(myObj.color);
}
}
Partial Classes
It is possible to split the definition of a class, a struct, and interface or a method over two ro more source files. Each source file contains a section of the type or method definition, and all parts are combined when the application is compiled.
//Partial Classes
using System;
public partial class Account
{
private int accountNo;
private string name;
private double balance;
}
public partial class Account
{
public void getData()
{
Console.WriteLine("Enter Name of the customer: ");
name = Console.ReadLine();
Console.WriteLine("Enter Account Number : ");
accountNo = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Balance Amount: ");
balance = double.Parse(Console.ReadLine());
}
public void displayData()
{
Console.WriteLine($"Name of the customer: {name} \n Account Number: {accountNo} \n Balance Amount: ${balance}");
}
}
Nested Classes
A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods (member function which defines actions) into a single unit.
In C#, a user is allowed to define a class within another class. Such types of classes are known as nested class. This feature enables the user to logically group classes that are only used in one place, thus this increases the use of encapsulation, and creates more readable and maintainable code.
// Concept of nested class
using System;
public class Outer_class
{
public void method1()
{
Console.WriteLine("Outer class method");
}
public class Inner_class
{
public void method2()
{
Console.WriteLine("Inner class Method");
}
}
}
public class GFG
{
static public void Main()
{
// Create the instance of outer class
Outer_class obj1 = new Outer_class();
obj1.method1();
// Creating an instance of inner class Note for the class name
Outer_class.Inner_class obj2 = new Outer_class.Inner_class();
// Accessing the method of inner class
obj2.method2();
}
}
Lists
List is a generic collection class provided by the .NET Framework that allows you to store and manipulate a dynamic collection of items. It provides a flexible and efficient way to work with collections of objects.
To use the List class, you need to include the System.Collections.Generic
namespace in your code.
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a new List of integers
List<int> numbers = new List<int>();
// Add items to the list
numbers.Add(10);
numbers.Add(20);
numbers.Add(30);
// Accessing elements by index
int firstNumber = numbers[0];
Console.WriteLine(firstNumber); // Output: 10
// Modifying elements
numbers[1] = 50;
Console.WriteLine(numbers[1]); // Output: 50
// Count property
int count = numbers.Count;
Console.WriteLine(count); // Output: 3
// Iterating over the list
foreach (int number in numbers)
{
Console.WriteLine(number);
}
// Output: 10, 50, 30
// Removing items
numbers.Remove(50);
Console.WriteLine(numbers.Count); // Output: 2
// Clearing the list
numbers.Clear();
Console.WriteLine(numbers.Count); // Output: 0
}
}
Leave a comment