Posts

Showing posts with the label C# Programming Language

Polymorhism in C#

Polymorphism (meaning "many forms") is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common type. In C#, polymorphism is achieved through two main mechanisms:   1. Compile-Time Polymorphism (Static Polymorphism/Overloading): This type of polymorphism is resolved at compile time. It's achieved through method overloading and operator overloading. Method Overloading: Defining multiple methods within the same class with the same name but different parameter lists (different number, types, or order of parameters). The compiler determines which method to call based on the arguments provided during the method call. C# public class Calculator { public int Add ( int a, int b ) { return a + b; } public double Add ( double a, double b ) { return a + b; } public int Add ( int a, int b, int c ) { return a + b + c; ...

Math in C#

C# Math: A Comprehensive Guide C# provides a robust Math class within the System namespace, offering a wide range of mathematical functions to simplify complex calculations. This class is static, meaning you can access its methods directly without creating an instance of the class. Commonly Used Math Functions: Here are some of the most frequently used mathematical functions:  * Trigonometric Functions:    * Math.Sin(double): Calculates the sine of an angle in radians.    * Math.Cos(double): Calculates the cosine of an angle in radians.    * Math.Tan(double): Calculates the tangent of an angle in radians.    * Math.Asin(double): Calculates the arcsine (inverse sine) of a value.    * Math.Acos(double): Calculates the arccosine (inverse cosine) of a value.    * Math.Atan(double): Calculates the arctangent (inverse tangent) of a value.    * Math.Atan2(double y, double x): Calculates the arctangent of y/x, taking int...

String Methods in C#

String Methods in C# C# provides a rich set of methods to manipulate strings, making it easy to perform various operations like searching, replacing, formatting, and more. Here are some of the most commonly used string methods: Basic String Operations Length: Returns the number of characters in the string. C# string str = "Hello, World!"; int length = str.Length; // length = 13 ToUpper() and ToLower(): Converts the string to uppercase or lowercase. C# string str = "hello, world!"; string upper = str.ToUpper(); // upper = "HELLO, WORLD!" string lower = str.ToLower(); // lower = "hello, world!" Trim(): Removes leading and trailing whitespace. C# string str = " Hello, World! "; string trimmed = str.Trim(); // trimmed = "Hello, World!" Substring(): Extracts a substring from the string. C# string str = "Hello, World!"; string substring = str.Substring(7, 5); // substring = "World" Searching and Replacing In...

Arrays in C#

  Arrays in C# In C#, arrays are a collection of variables that share the same data type. They provide a convenient way to store and manipulate multiple values under a single name. Types of Arrays in C# Single-Dimensional Arrays: A linear collection of elements, accessed using a single index. Declaration: C# int [] numbers = new int [ 5 ]; // Declares an array of 5 integers Initialization: C# int [] numbers = { 1 , 2 , 3 , 4 , 5 }; // Initializes with values Access: C# int firstNumber = numbers[ 0 ]; // Access the first element Multi-Dimensional Arrays: Arrays with multiple dimensions, accessed using multiple indices.