- read

Getting Started with Go — Object-Oriented Programming (c# & Go compared), defer, panic & recover

N Nikitins 60

Getting Started with Go — Object-Oriented Programming (c# & Go compared), defer, panic & recover

N Nikitins
Level Up Coding
Published in
7 min read4 days ago

--

Greetings, fellow developers! 👋 In this post, we’re embarking on an exciting journey to explore the world of Object-Oriented Programming (OOP) in two fantastic languages: Go and C#. Our mission is to compare the OOP concepts in both languages, discover their strengths, and examine how they’re implemented. So, without further ado, let’s dive in and unravel the mysteries of OOP in Go and C#!

Previously on …

Object-Oriented Programming in C#

C# is known for its robust OOP capabilities, making it an excellent language for building complex applications. Here’s a quick overview of some of the key OOP concepts in C#:

Classes and Objects

In C#, classes are the blueprints for creating objects. Each object is an instance of a class and contains both data (attributes) and methods (functions). Let’s look at an example:

class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }

public string GetFullName()
{
return FirstName + " " + LastName;
}
}

// Creating an object
Person person = new Person();
person.FirstName = "John";
person.LastName = "Doe";
string fullName = person.GetFullName();

Inheritance

C# supports inheritance, allowing you to create a new class (derived or child class) based on an existing class (base or parent class). Inherited classes inherit the properties and behaviors of their parent classes.

class Employee : Person
{
public double Salary { get; set; }
}

Employee employee = new Employee();
employee.FirstName = "Jane";
employee.LastName = "Smith";
employee.Salary = 50000.00;

Polymorphism

Polymorphism in C# enables objects of different classes to be treated as objects of a common base class. It’s implemented through method…