Saturday, January 20, 2024

Object Oriented Programming- 2- Inheritance

Inheritance is one of the fundamental attributes of object-oriented programming.

It allows you to define a child class that reuses (inherits), extends, or modifies the behavior of a parent class.

The class whose members are inherited is called the base class.

The class that inherits the members of the base class is called the derived class.

Note: Above content is from Microsoft Learning.


Creating classes for our programming real world problems is useful. But Inheritance on the other hand is very useful to solve many other programming real world problems and will let yuou create more efficient code.

Lets try this with practical example.

Saturday, December 30, 2023

Object Oriented Programming- 1

C# is an object oriented programming language.

The four basic principles of object-oriented programming are:

  • Abstraction Modeling the relevant attributes and interactions of entities as classes to define an abstract representation of a system.
  • Encapsulation Hiding the internal state and functionality of an object and only allowing access through a public set of functions.
  • Inheritance Ability to create new abstractions based on existing abstractions.
  • Polymorphism Ability to implement inherited properties or methods in different ways across multiple abstractions.

Note: Above content is from Microsoft Learning.

Let's try this with some practical examples.

Friday, December 29, 2023

ToString method in Object class

In .Net, all the data types are derived from Object data type. Object data type has a method named ToString, which is overridable and provides a basic implementation of converting any object to String.

By default ToString returns the objects Class name along with its namespace.

Example:

namespace HelloWorld;
class Employee {
}
class Program
{
  static void Main(string[] args)
  {
    var emp = new Employee();
    Console.WriteLine(emp.ToString());
  }
}

Output:

HelloWorld.Employee

As all the numbers are derived from Object data type and ToString is overridable, .net has a overridden implementation of ToString for Numbers, which converts them to String.

var a = 123;
Console.WriteLine(a);

Output:

123

Wednesday, December 20, 2023

Using CultureInfo for Localization

As the computer programs are worldwide and the world has so many countries and languages and cultures, it is an important topic to understand the .Net Capability to support it.

.Net has a class call CultureInfo available in System.Globalization namespace to use the most of the cultures available in the world.

We can use these cultures for various localization purpose such as currency formatting.

To list all the supported CultureInfo, we can use the following code snippet.

var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
foreach (var culture in cultures)
{
  Console.WriteLine(culture.DisplayName + "\t"
    + culture.Name + "\t"
    + (12001).ToString("C", culture));
}

Above code uses the culture to convert the number to currency format in particular culture.

For Example, en-US culture will convert 12001 to $12,001.00

We can pick a particular culture with its code

For Example to pick the en-US culture and use it, we can use the below code snippet.

var culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
Console.WriteLine((12001).ToString("C", culture));

Tuesday, December 19, 2023

Primitive types in C#

Following are primitive data types used in C#

Purpose C# Data Type .Net Data Type
Text char System.Char
string System.String
Fixed Point Signed Numbers sbyte System.SByte
short System.Int16
int System.Int32
long System.Int64
Fixed Point Unsigned Numbers byte System.Byte
ushort System.UInt16
uint System.UInt32
ulong System.UInt64
Floating Point Numbers float System.Single
double System.Double
decimal System.Decimal
Boolean bool System.Boolean

Declaring and initializing a variable

Develop On Post

In C#, you should declare a variable before using a variable.

You can declare variable using the following syntax.

Syntax:

{datatype} {variable-name};

Example:

int i;

Assigning a value while declaring is called Initialization.

Syntax:

{datatype} {variable-name} = {expession};

Example:

int i = 100;

Here the expression in the right side is of the type 'int'. So having it in the left side might feel reduntant.

C# has a feature called "Implicitly typed local variables", through which we can avoid this.

Syntax:

var {variable-name} = {expession};

Example:

var i = 100;

This var keyword will work only inside the code block, not at class member level.

If it is at the class member level, C# compiler will through the following error during compilation.

The contextual keyword 'var' may only appear within a local variable declaration or in script code.

Saturday, December 16, 2023

Floating Point Numeric Data types

.Net supports three floating point numeric data type.

Below are all the floating point numeric data types supported by .net and their corresponding c# data types

You can choose one of these types for you variables and constants depending upon your requirement (range of the data and its precision).

Decimal (decimal) is high precision but with less range.

Single (float) and Double(double) are low precision but with high range.

Calculation performance of Decimal is much slower that Float and Double.

.Net Type C# Type Size in bytes Range Precision Suffix
System.Single float 4 ±1.5 x 10-45 to ±3.4 x 1038 ~6-9 digits F/f
System.Double double 8 ±5.0 x 10-324 to ±1.7 x 10308 ~15-17 digits D/d
System.Decimal decimal 16 ±1.0 x 10-28 to ±7.9228 x 1028 28-29 digits M/m

When using a const floating point number in the program by default it is considered as double.

If you want to change its type to float or decimal, you can use the suffix

Example.

var len = 245.5; // double
var size = 245.5F; // float
var age = 245.5M; // decimal

Object Oriented Programming- 2- Inheritance

Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class...