Tuesday, March 15, 2022

oops

 Functional programming has the following problems.

  1. Reusability
  2. Extensibility
  3. Simplicity
  4. Maintainability

 

Reusability:

In Functional Programming, we need to write the same code or logic at multiple places which increases the code duplication. Later if we want to change the logic, then we need to change it at all places.

Extensibility:

It is not possible in functional programming to extend the features of a function. Suppose you have a function and you want to extend it with some additional features then it is not possible. You have to create a completely new function and then change the function as per your requirement.

Simplicity:

As extensibility and reusability are not possible in functional programming, usually we end up with lots of functions and lots of scattered code.

Maintainability:

As we don’t have Reusability, Extensibility, and Simplicity in functional Programming, so it is very difficult to manage and maintain the application code.

 

 

How we can overcome Functional Programming Problems?

We can overcome the functional programming problems (Reusability, Extensibility, Simplicity, and Maintainability) using Object-Oriented Programming. OOPs provide some principles and using those principles we can overcome the functional programming problems.

What Is Object-Oriented Programming?

Let us understand Object-Oriented Programming i.e. OOPs Concepts using C#. Object-Oriented Programming (OOPs) in C# is a design approach where we think in terms of real-world objects rather than functions or methods. Unlike procedural programming language, here in oops, programs are organized around objects and data rather than action and logic. Please have a look at the following diagram to understand this better.

 

 


Reusability:

To address reusability, object-oriented programming provides something called Classes and Objects. So, rather than copy-pasting the same code again and again in different places what you can do here is, create a class and make an instance of the class which is called object, and reuses them whenever you want.

Extensibility:

Suppose you have a function and you want to extend it with some new features that were not possible with functional programming. You have to create a completely new function and then change the whole function whatever you want. In OOPs, this problem is addressed by using some concepts called Inheritance, Aggregation, and Composition. In our upcoming article, we will discuss all these concepts in detail.

Simplicity:

Because we don’t have extensibility and reusability in functional programming, we end up with lots of functions and lots of scattered code. In OOPs, this problem is addressed by using some concepts called Abstraction, Encapsulation, and Polymorphism.

Maintainability:

As OOPs address Reusability, Extensibility, and Simplicity, we have good maintainable code and clean code which increases the maintainability of the application.

What are the OOPs Principles or OOPs Concepts in C#?

OOPs, provide 4 principles. They are

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

 

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:

  • 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.

Class :- Car

Object volvo ,Audi,Toyota

Accept

a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the variables and methods from the class.

 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.

Methods normally belongs to a class, and they define how an object of a class behaves.

Just like with fields, you can access methods with the dot syntax. However, note that the method must be public. And remember that we use the name of the method followed by two parantheses () and a semicolon ; to call (execute) the method:

The public keyword is called an access modifierThe public keyword is an access modifier, which 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. 

 

Sunday, March 13, 2022

Array

 

multidimensional arrays up to 32 dimensions. The multidimensional array can be declared by adding commas in the square brackets. For example, [,] declares two-dimensional array, [, ,] declares three-dimensional array, [, , ,] declares four-dimensional array, and so on. So, in a multidimensional array, no of commas = No of Dimensions - 1.

The following declares multidimensional arrays.

int[,] arr2d; // two-dimensional array

int[, ,] arr3d; // three-dimensional array

int[, , ,] arr4d ; // four-dimensional array

int[, , , ,] arr5d; // five-dimensional array

 

 

 

int[,] arr2d = new int[3,2]{ 
                                {1, 2}, 
                                {3, 4}, 
                                {5, 6} 
                            };
 
// or 
int[,] arr2d = { 
                    {1, 2}, 
                    {3, 4}, 
                    {5, 6} 
                };

 

 nt[,] arr2d = new int[3, 2]{

        {1, 2},
        {3, 4},
        {5, 6}
       };

   Console.WriteLine(arr2d[0, 0]);
   Console.WriteLine(arr2d[0, 1]);
   Console.WriteLine(arr2d[1, 0]);
   Console.WriteLine(arr2d[1, 1]);
   Console.WriteLine(arr2d[2, 0]);
   Console.WriteLine(arr2d[2, 1]);
   int[,,] arr3d1 = new int[1, 2, 2]{
   {{1, 2}, {3, 4}} // 1 row of two-dimensional array
  };

   int[,,] arr3d2 = new int[2, 2, 2]{ // 2 rows of two-dimensional array
   {{1, 2}, {3, 4}},
   {{5, 6}, {7, 8}}
  };

   int[,,] arr3d3 = new int[2, 2, 3]{ // 2 rows of two-dimensional array
   {{1, 2, 3}, {4, 5, 6}},
   {{7, 8, 9}, {10, 11, 12}}
  };

   Console.WriteLine("arr3d2 Values");
   Console.WriteLine(arr3d2[0, 0, 0]);
   Console.WriteLine(arr3d2[0, 0, 1]);
   Console.WriteLine(arr3d2[0, 1, 0]);
   Console.WriteLine(arr3d2[0, 1, 1]);
   Console.WriteLine(arr3d2[1, 0, 0]);
   Console.WriteLine(arr3d2[1, 0, 1]);
   Console.WriteLine(arr3d2[1, 1, 0]);
   Console.WriteLine(arr3d2[1, 1, 1]);


 

 

 

A jagged array is an array of array. Jagged arrays store arrays instead of literal values.

A jagged array is initialized with two square brackets [][]. The first bracket specifies the size of an array, and the second bracket specifies the dimensions of the array which is going to be stored.

 

 

 

int[][] jArray1 = new int[2][]; // can include two single-dimensional arrays 
int[][,] jArray2 = new int[3][,]; // can include three two-dimensional arrays 

 

 

int[][] jArray = new int[2][]; 
 
jArray[0] = new int[3]{1, 2, 3};
 
jArray[1] = new int[4]{4, 5, 6, 7 };

 

 // int[][] jArray = new int[2][]{

// new int[3]{1, 2, 3},


// new int[4]{4, 5, 6, 7}

//};


// Console.WriteLine(jArray[0][0]);

// Console.WriteLine(jArray[0][1]);

// Console.WriteLine(jArray[0][2]);

// Console.WriteLine(jArray[1][0]);

// Console.WriteLine(jArray[1][1]);

// Console.WriteLine(jArray[1][2]);

// Console.WriteLine(jArray[1][3]);


//int[][] jArray = new int[2][]{

// new int[3]{1, 2, 3},


// new int[4]{4, 5, 6, 7}

//};


//for (int i = 0; i < jArray.Length; i++)

//{

// for (int j = 0; j < (jArray[i]).Length; j++)

// Console.WriteLine(jArray[i][j]);

//}