Types, DS & Algol in .NET

raviklog

raviklog

Posted on January 10, 2023

Types, DS & Algol in .NET

Hi Folks! please check out my previous post, which deals with basic building blocks of .NET and how this current post followed from there


Types are the building blocks of programming language, and it allows to define the variable type or an expression. It defines the set of values that a variable can take on and the operations that can be performed on those values. So, this aids the C# compiler to perform type checking, ensure type safety, and select the correct method overload at compile-time.

In .NET all of the types basically reach at the top of hierarchy "System. Object" but falls under either Value or Reference Types.

type hierarchy

So still there is a need to arrange these types in a manner in which simple and composite level can be achieved. So that's where the classification of Primary/Primitive type and Secondary/Derived types comes into play

These Primitive types are basically Scalar (or single valued) in nature and its useful for handling simple values (numbers, characters, boolean and so on) ...

Integral types: sbyte, byte, short, ushort, int, uint, long, ulong
Floating-point types: float, double
Decimal type: decimal
Boolean type: bool
Character type: char

Just if we know only these types can we design an application...Let's say we create a calculator/ Tic-Tac-Toe game...so in this kind of scenarios...it's possible to use just the primitive types to create an application, but then it's limited and we can't create more intuitive and valuable applications by just using primitives...They can be basic foundation, but we need more elaborate types to handle extended applications.

Then comes the Secondary Data types/Composite types, set of value types that are built-in to the framework and are used to represent different types of data. Examples of secondary data types in .NET as given below,

Image description An array is a collection of items of the same data type that are stored in contiguous memory locations.

Image description A string is a sequence of characters and are immutable. This means that once a string is created, its value cannot be modified. Instead, any operation that would normally modify a string in-place will actually create a new string with the modified value.

Image descriptionAn enumeration is a set of named constants. Enumerations are useful when you have a fixed set of values that a variable can take on, such as the days of the week, or the suit in a deck of cards.

A Structure is a value type that can contain a set of fields and methods. It is normally used to represent simple logic within an application. (For Example: below I have declared a default container with properties width and height and a method to calculate size of the container on its own. Simple data storage and logic could be done through struct.

public interface ISampleContainer
{
  public double GetContainerSize();
}
struct TestContainer: ISampleContainer
{
   private double width.
   private double height.
   private double magicNumber;
   public double GetContainerSize()
   {
     return (width * height * magicNumber);
   }
}
Enter fullscreen mode Exit fullscreen mode

(Structures do perform encapsulation as above with members and functions, does an interface inheritance, but structures do not allow implementation inheritance from classes or other structures...So basically, the hierarchical system could not be formed through structures. So, we cannot have a parent structure/class for another structure.)

Class: A Class is a reference type that can contain a set of fields, methods, and events. It is also called a user defined type as it takes it existence based on the developer's necessity to handle the characteristics and behavior of objects of that type.
The True power of classes is it is the main way to exhibit OOPs concepts (Inheritance, Encapsulation, Abstraction & Polymorphism) ...Detailed post on OOPs will do it another post with samples.

class MyTestContainer: ISampleContainer
{
   public double width.
   public double height.
   public double containerFactor;

   public virtual double GetContainerSize()
   {
     return (width * height * containerFactor);
   }
}
class CircleContainer:MyclassContainer
{
   public double Radius {get;set;}
   public override double GetContainerSize()
   {
     return Math.PI * Radius * Radius;
   }
}
Enter fullscreen mode Exit fullscreen mode

(Class "CircleContainer" does exhibit the Inheritance concept where it overrides the default base method GetContainerSize of Parent class MyTestContainer)

Do .NET provide some pre-defined structures and classes?
Yes, there are quite a number of them, some of most commonly used
Structures and Classes in FCL,

Image description
Image description

Data types define the kind of data that can be stored in a variable or expression, while data structures define how that data is organized and stored in memory. In case of large sets of data for an application, then we need efficient way to organize the data, so that certain operations like Analysis, Retrieval can be done quicker and optimal way.

Here are a few common data structures in .NET:

Image description

As a developer we have the types and data structures to store and organize data for managing the application-level information, but
Algorithms are used to solve problems and perform tasks in a systematic and efficient way.

Here are a few common algorithms that are frequently used in .NET development:

Sorting algorithms: Sorting algorithms are used to rearrange a list of items in a specific order, such as ascending or descending. Examples of sorting algorithms include bubble sort, selection sort, and quicksort.

Search algorithms: Search algorithms are used to find a specific item in a list or collection. Examples of search algorithms include linear search and binary search.

Graph algorithms: Graph algorithms are used to traverse and manipulate graphs, which are data structures that consist of vertices (nodes) and edges. Examples of graph algorithms include depth-first search and breadth-first search.

Recursion: Recursion is a technique in which a function calls itself to solve a problem. It is often used to solve problems that can be broken down into smaller, simpler subproblems.

Dynamic programming: Dynamic programming is a technique for solving problems by breaking them down into smaller subproblems and storing the results in a table to avoid redundant calculations.

By understanding and using these types, data structures and algorithms, we can write more efficient and effective code in .NET.

DS & Algos are quite an effective topic, I will try adding more posts to have clarity on the concepts and also do some code samples on the same.

Please do convey your comments/suggestions, it will help me to improve the content overall!

💖 💪 🙅 🚩
raviklog
raviklog

Posted on January 10, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Types, DS & Algol in .NET
dotnet Types, DS & Algol in .NET

January 10, 2023