Functions (Methods) in C#: Built-in

Mannan Ul Haq
0

C# is a powerful programming language that offers a wide range of built-in methods and functions to simplify programming tasks and provide ready-to-use functionality. These built-in methods/functions are part of the .NET Framework and cover various areas such as console input/output, mathematical operations, string manipulation, working with arrays, and more. In this response, we will explore some of the important and commonly used built-in methods/functions in C#.


1. `System.Math`:

Method Description Example
Abs() Returns the absolute value of a number. int absoluteValue = Math.Abs(-10); // Result: 10
Ceiling() Rounds a decimal value to the nearest greater or equal integer. double roundedUp = Math.Ceiling(4.3); // Result: 5
Floor() Rounds a decimal value to the nearest smaller or equal integer. double roundedDown = Math.Floor(4.8); // Result: 4
Max() Returns the larger of two numbers. int largest = Math.Max(10, 20); // Result: 20
Min() Returns the smaller of two numbers. int smallest = Math.Min(10, 20); // Result: 10
Pow() Calculates the power of a number. double result = Math.Pow(2, 3); // Result: 8
Round() Rounds a decimal value to the nearest integer or a specified number of decimal places. double roundedValue = Math.Round(4.6); // Result: 5
Sqrt() Calculates the square root of a number. double result = Math.Sqrt(25); // Result: 5


2. `System.String`:

Method Description Example
Compare() Compares two strings and returns an integer that indicates their relative position in the sort order. int comparisonResult = String.Compare("apple", "banana"); // Result: -1
Concat() Concatenates one or more strings into a single string. string firstName = "John"; string lastName = "Doe"; string fullName = String.Concat(firstName, " ", lastName); // Result: "John Doe"
Length Returns the length of the string. string str = "Hello"; int length = str.Length; // Result: 5
Substring() Returns a substring of the original string. string str = "Hello World"; string subStr = str.Substring(6); // Result: "World"
ToLower() Converts the string to lowercase. string str = "Hello"; string lowerStr = str.ToLower(); // Result: "hello"
ToUpper() Converts the string to uppercase. string str = "Hello"; string upperStr = str.ToUpper(); // Result: "HELLO"
Trim() Removes leading and trailing white spaces from the string. string str = " Hello "; string trimmedStr = str.Trim(); // Result: "Hello"
IndexOf() Returns the index of the first occurrence of a substring. string str = "Hello World"; int index = str.IndexOf("World"); // Result: 6


3. `System.Array`:

Method Description Example
Sort() Sorts the elements of an array. int[] numbers = { 5, 2, 1, 3, 4 }; Array.Sort(numbers);
Length Returns the length of the array. int[] numbers = { 5, 2, 1, 3, 4 }; int length = numbers.Length;
Reverse() Reverses the sequence of the elements in the entire one-dimensional Array. int[] numbers = { 5, 2, 1, 3, 4 }; Array.Reverse(numbers); // numbers: {4, 3, 1, 2, 5}
Copy() Creates a copy of the Array. int[] numbers = { 5, 2, 1, 3, 4 }; int[] copyOfNumbers = new int[numbers.Length]; Array.Copy(numbers, copyOfNumbers, numbers.Length);
BinarySearch() Searches an array for a specific value using the binary search algorithm. int[] numbers = { 1, 2, 3, 4, 5 }; int index = Array.BinarySearch(numbers, 3); // index: 2


4. `System.DateTime`:

Method Description Example
Now Gets the current date and time. DateTime now = DateTime.Now;
AddDays() Adds a specified number of days to a date. DateTime tomorrow = DateTime.Now.AddDays(1);
ToString() Converts the date and time value to a string representation. DateTime now = DateTime.Now; string dateString = now.ToString("yyyy-MM-dd HH:mm:ss");


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !