Learn about Strings and Functions in C# Programming

0
1031
c#

c#

In this article, we are going to talk about very important data type which is Strings. After that we will introduce Functions. A rich article that you should read.

Working with strings

C# treats strings as an array of type char. Accordingly, you can access string elements the same way you access array members. Consider the following example:

The output of this code will be ‘d’. The string “Ahmed” is treated as a char array containing 5 characters, with ‘A’ at position 0, and d at position 4.
Since strings are a type of arrays, you can use array operations on them. For example, Console.WriteLine (myName.Length); will return 5, which is the number of characters in the string.
Now let’s see some other useful operations that C# provides, and can be applied on strings:

Changing case

The .ToLower() and .ToUpper() methods are used to change the letter case of the string to upper or lower respectively. You may find those methods extremely useful when you want to ensure that the user has entered the correct text regardless of the letter case used. Consider the following example: you built a secure application that requires users to login to their accounts first before accessing the application. One of the users entered the username like this: “JohnDoe”. But the username saved in your records is “johndoe”. If you tried to test whether or not the username was entered correctly, the test will fail because C# is case sensitive. So you can either inform the users to always enter their data in lower case, or you can provide a better user experience by using .ToLower() method as follows:

Now observe the program behavior:

Now have a look at the modified version:

Now when the user tries to access the application, access will be granted no matter in which case the username was entered:

Trimming spaces

Another useful method is Trim(). It is used to remove any whitespace at the beginning and at the end of the sting. Again, this can be used in the application of the previous example; the user may enter the username followed by a space or preceded by one. You can never trust a user’s input. Let’s modify our application to tolerate whitespaces in the username by removing them before testing for equality with the saved username:

The changed part here is the Trim() method that was added to the string. Another thing to notice is that you chained the methods. That is, you applied Trim() to the string resulting from the ToLower() method. Chaining methods is one useful feature of C# that will save you time and code.

String padding

This method is one way to make numbers look pretty or consistent. For example, when you’re printing a list containing numbers, sometimes you want to pad them with zeroes so that they all share the same number of digits. PadLeft() and PadRight() methods do just that. Have a look at the following:

The output of the above code is:

As you can see, the method takes two arguments: the number of characters to pad, and the character you wish to use. Note that the second argument must be of type char. If you used a string instead, an error will be raised. PadRight() does the same thing but adds the padding to the right of the string. It’s worth noting that if PadLeft() or PadRight() was used without the second argument, a whitespace will be used to pad the string.

C# Functions

A function is a block of code that does a specific task. When called, the block of code gets executed. It may or may not return an output. It also may accept zero or more parameters. Consider the following example:

The output of the above code will be: “Hello”. Here you created a function called greet(). This function will output the string “Hello” to the screen. To invoke the function, you just write its name followed by a double of parentheses. Let’s have a look at this function’s anatomy:

  1. It started with void which means that the function will not return an output (ignore static for the moment)
  2. Then it used an empty pair of parentheses because it does not accept parameters
  3. The function body starts with a curly brace and ends with one

Learn Cloud Computing from Scratch for Beginners

Functions return values

You can use functions to output values. For example, a function that is used to add two integers must have an output of type integer or double. You use the return keyword to instruct the function to give back the value. To do this, you have to specify the function’s return type in the definition. Have a look at the following example:

static String getUsername(){
return “johndoe”;
}

public static void Main (string[] args)
{
Console.WriteLine (getUsername ());
}

The output of this code is “johndoe”. Notice that we used String instead of void when defining the function.

As soon as the function execution reaches the word return, the function exits. The return keyword can also be used in void functions. Because of this behavior, this keyword can be used to force the function to exit before reaching the end of its code, which is often used when using if-conditions and loops.

Function parameters

Functions are most powerful when they process parameters. You can pass parameters to a function by adding them in the parentheses. Just make sure that you enter the variable type before the parameter, and when calling the function, you must provide the same number of parameters, and they must be of the correct type. For example, the greet() that you created earlier needs an important improvement: a name to greet. This name can be passed as a string parameter as follows:

The output of this code will be: “Hello Ahmed”. Try and replace “Ahmed” in the greet function call with any other string and it will display “Hello” followed by this string. The sName here is a function parameter. It becomes immediately available to the function body once the function is called. You can pass any number of parameters separated by commas. For example, the following function is used to add two integers and provide the result:

Conclusion

In this article, we studied some of the powerful methods that C# uses to manipulate strings. Then, we started our discussion of functions, how they are defined, called, and the different forms a function might take.

In the next article we’ll continue demonstrating functions. You’ll learn more forms of passing variables to functions; you’ll also differentiate between reference type and value type parameters. We’ll also cover variable scopes, and when the variable is accessible to your code and when it isn’t.

I hope you enjoyed reading this post, see you next time.

LEAVE A REPLY

Please enter your comment!
Please enter your name here