Inbuilt Functions
Objectives
• Inbuilt Functions
• String Functions (The Mid, Right, Left & Trim methods)
• String Manipulation Using + and & signs.
• Mathematical Functions (Abs, Exp, Fix functions)
• Formatting Functions (ConvertToString, ConvertToInt16… Methods; Date and
Time Formatting functions)
• User-Defined Functions
• Passing parameters by value & by reference
Inbuilt Functions
• In C# programming language, inbuilt functions are predefined functions
that are provided by the .NET framework or other libraries.
• These functions are ready to use and can be called directly in your code
without the need to define them yourself.
• Inbuilt functions are designed to perform common tasks or operations,
such as mathematical calculations, string manipulation, file handling, and
more.
• Some examples of inbuilt methods in C# include:
• `Console.WriteLine()` for outputting text to the console,
• `Math.Max()` for finding the maximum of two numbers, and
• `String.ToUpper()` for converting a string to uppercase.
String Functions
• Here are some common methods of the `String` class in C#:
1. `Mid / Substring(int startIndex)`: Returns a substring starting from the specified index.
2. `Trim()`: Removes leading and trailing white spaces from the string.
3. `Right`: Get specific number of characters (count) from right side
4. `Left`: Get specific number of characters (count) from left part
5. `Length`: Returns the length of the string.
6. `ToUpper()`: Converts the string to uppercase.
7. `ToLower()`: Converts the string to lowercase.
8. `Contains(string value)`: Checks if the string contains a specified value.
9. `IndexOf(char value)`: Returns the index of the first occurrence of the specified character.
Left, right, and mid strings
• Unlike other programming languages C# doesn’t have special
methods for the left, right or mid part of a string but we can still
implement that behavior.
• We do so using the C#’s substring method
Left part of string
string.Substring(0, count)
• 0 arg: specifies we are starting from the left side.
• Count arg: specifies the number of characters from the string’s left
side
string example = "Hello, World!";
// Get the 5 characters from the left of the string
string exampleLeft = example.Substring(0, 5);
// Output
Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Left string: {exampleLeft}");
Here is how the output looks like:
Original string: Hello, World!
Left string: Hello
Right Part of A String
• string.Substring(string.Length - num_characters_wanted, characters_to_return)
H E L L O J O S E
• String.Substring(string.Length - 4, 4);
string example = "HELLO JOSE";
// Get 4 characters from the right of the string
string exampleRight = example.Substring(example.Length - 4, 4);
// Output Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Right string: {exampleRight}");
Here is how the output looks like:
Original string: HELLO JOSE
Left string: JOSE
Mid Part of String
• The programming languages with a Mid() string method often provide
two versions.
• One that returns a string segment that begins at some character index.
• The other returns a certain number of characters starting from a character
index.
• We shall explore how we implement both in C#.
• To get the mid part of a string that begins at a specific index
• we call the Substring() method on that string.
• We provide that method with one argument: the start index.
• The pattern for that looks like: input.Substring(index).
• This has the string method return all characters from that index till the end of
that string.
Mid Part of String
• First Version
string example = "Hello, World!";
// Get all characters from the 4th character onward
string exampleMid = example.Substring(4); \
// Output
Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Mid string: {exampleMid}");
• Output
Original string: Hello, World!
Mid string: o, World!
Mid Part of String
• Second Version:
string example = "Hello, World!";
// Get 6 characters from the string, starting at character 4
string exampleMid = example.Substring(4, 6);
// Output
Console.WriteLine($"Original string: {example}");
Console.WriteLine($"Partial mid string: {exampleMid}");
• Output:
Original string: Hello, World!
Partial mid string: o, Wor
Trim()
• Illustrating the trim method:
string name = “ joseph“;
string name1.Trim();
String Concatenation using + operator
string str1 = "Hello";
string str2 = "World";
//The following line Concatenates two strings with a space in between
string result = str1 + " " + str2;
Console.WriteLine(result);
• // Output:
Hello World
String Manipulation Using + operator
string str = "Hello";
str += " World"; // Appends " World" to the existing string
Console.WriteLine(str);
// Output:
Hello World
String Manipulation using the & operator
• string str = "Hello";
• str &= " World"; // Concatenates " World" to the existing string
• Console.WriteLine(str);
• // Output:
Hello World
Mathematical Functions (Abs, Exp, Fix functions)
Abs: returns the absolute value of a given number
1. int num = -10;
2. int absNum = Math.Abs(num);
3. Console.WriteLine(absNum);
// Output: 10
Mathematical Functions (Abs, Exp, Fix functions)
• Exp: returns e raised to the power of given number
1. double x = 2;
2. double result = Math.Exp(x);
3. Console.WriteLine(result);
// Output:
7.38905609893065
Mathematical Functions (Abs, Exp, Fix functions)
• the Fix function is used to truncate a floating-point number to its
integer part, effectively rounding towards zero.
• The Fix function removes the fractional part of a number
• In C#, there is no built-in Fix function like in some other programming
languages. However, you can achieve similar functionality using the
Math.Floor or Math.Ceiling functions depending on your
requirements.
• Truncate: Remove, cut off
Mathematical Functions (Abs, Exp, Fix functions)
• Using Math.Floor for rounding down:
• The Math.Floor function returns the largest integer less than or
equal to the specified number.
• This can be used to achieve similar functionality to the Fix function
in other languages.
1. double num = 3.7;
2. double fixedNum = Math.Floor(num);
3. Console.WriteLine(fixedNum);
• // Output: 3
Mathematical Functions (Abs, Exp, Fix functions)
• Using Math.Ceiling for rounding up:
• The Math.Ceiling function returns the smallest integer greater than
or equal to the specified number.
• This can also be used for similar functionality to the Fix function.
1. double num = 3.2;
2. double fixedNum = Math.Ceiling(num);
3. Console.WriteLine(fixedNum);
// Output: 4
Formatting Functions (ConvertToString,
ConvertToInt16… Methods; Date and Time
Formatting functions)
• You can use various formatting functions to convert data types, format
strings, and work with date and time values. Here are some commonly
used formatting functions in C#:
• ConvertToString method:
• The Convert.ToString method is used to convert a value to its string
representation.
1. int num = 10;
2. string str = Convert.ToString(num);
3. Console.WriteLine(str);
4. // Output: "10"
Formatting Functions (ConvertToString,
ConvertToInt16… Methods; Date and Time
Formatting functions)
• Date and Time Formatting functions:
• C# provides various methods for formatting date and time values using the
DateTime structure and ToString method
1. DateTime thisDay= DateTime.Now;
2. string formattedDate = thisDay.ToString("yyyy-MM-dd HH:mm:ss");
3. Console.WriteLine(formattedDate);
// Output: "2023-03-08 07:30:45"
• ConvertToInt16 method:
• The Convert.ToInt16 method is used to convert a value to a 16-bit signed integer.
1. string str = "123";
2. short num = Convert.ToInt16(str);
3. Console.WriteLine(num);
// Output: 123
User-Defined Functions
Passing parameters by value & by reference
• In C#, you can define user-defined functions to encapsulate a block of code that performs a
specific task.
• Parameters can be passed either by value or by reference
• Passing By value (A copy of the original is passed to the function ):
void IncrementValue(int num)
{
num++;
}
int number = 5;
IncrementValue(number);
Console.WriteLine(number);
// Output: 5 (original value remains unchanged)
User-Defined Functions
Passing parameters by value & by reference
• Passing Parameters by Value:
• By default, when you pass a parameter to a function in C#, it is passed by
value. This means that a copy of the parameter's value is passed to the
function, and any changes made to the parameter within the function do not
affect the original value outside the function.
User-Defined Functions
Passing parameters by value & by reference
• Passing By Reference (original var is altered by the finction):
void IncrementValue(ref int num)
{
num++;
}
int number = 5;
IncrementValue(ref number);
Console.WriteLine(number);
// Output: 6 (original value is modified)
User-Defined Functions
Passing parameters by value & by reference
• Passing Parameters by Reference:
• You can pass parameters by reference using the ref or out keywords in C#. When a
parameter is passed by reference, any changes made to the parameter within the
function will affect the original value outside the function.
• NOTE:
• When using ref, the parameter must be initialized before passing it to
the function, and the function can read and modify the parameter.
• When using out, the parameter does not need to be initialized before
passing it to the function, but the function must assign a value to the
parameter before it returns.