Lesson Weekend

We wrote many JavaScript methods and functions in Introduction to Programming. The process is fairly similar in C# as well, but in this case we will always call them methods instead.

In this lesson, we'll practice calling built-in C# methods and learn about how C# methods are different from methods and functions in JavaScript. In an upcoming lesson, we'll learn how to write custom methods.

C# Methods


Here are some examples of built-in methods:

> string phrase = "Programming is AWESOME";

> phrase
"Programming is AWESOME"

> phrase.ToUpper()
"PROGRAMMING IS AWESOME"

> phrase.ToLower()
"programming is awesome"

The pattern for calling a C# method is similar to JavaScript:

  1. We append a ., or dot, to the end of our variable (or data we'd like to manipulate, if it's not stored in a variable).

  2. We list the method name after the dot. In the first example above, we used the built-in method ToUpper. In the second, we used ToLower.

  3. Just like with JavaScript, we append a set of parentheses () to the end of the method name. Often called parens for short, these are where arguments go. Sometimes methods have arguments. Sometimes they don't. The examples above don't contain arguments.

  4. The method then does something with or to the data it's called on. In this example, ToUpper() makes our string all uppercase whereas ToLower() makes it all lowercase.

Method Naming Convention

Notice that unlike JavaScript, methods in C# begin with a capital letter. The beginning of any other words in the method name are also capitalized such as LikeThis() or EvenLikeThisExampleHere(). This is called pascal case and is standard, required convention in C#.

Method Arguments

Let's look at a few more methods. This time we'll use some that take arguments:

> string phraseOne = "hello world";
> string phraseTwo = "hello";

> phraseOne.Contains(phraseTwo)
true

To review, arguments are pieces of data passed into a method. In the example above, Contains() is a string method. We pass it one string as an argument. It then determines if phraseTwo is included in phraseOne. In this case, “hello world” contains the phrase, or substring, "hello", so it returns true.

Similarly, we could use Replace() to swap out part of a string:

> string phraseOne = "hello world";
> string phraseTwo = "hello";

> string phraseThree = phraseOne.Replace("hello", "goodbye");

> phraseThree
"goodbye world"

Replace() takes two arguments. Both are strings. The method finds all substrings that match the first argument "hello" and then replaces these substrings with the second argument "goodbye", turning the string "hello world" into "goodbye world".

Strong Typing in Arguments

Because C# is a strongly-typed language, when we pass an argument to a method, it must be the correct type. For example, the Replace() method only accepts string arguments. If we try to pass it an integer (or int) instead, we get an error:

> string helloWorldString = "hello world";

> helloWorldString.Replace("hello", 1);

(1,26): error CS1503: Argument 1: cannot convert from 'string' to 'char'
(1,35): error CS1503: Argument 2: cannot convert from 'int' to 'char'

1 is an integer, or int data type, not a string. Errors like this one are very common. Whenever we see a message stating something like Cannot convert (some data type) to (some other data type), it means we've provided an incorrect data type in our code. The second data type mentioned in the message (char in the example above) is the data type C# expected; the first (int or string in this example) is the incorrect type we provided instead.

If we ever need to find out what arguments may be provided to string methods, we can look at the C# String Documentation.

Integer Methods

Integers also have built-in methods but there are not very many beyond the mathematical operators covered in the Integers and Arithmetic lesson. Here's one that converts an integer to a string:

> int number = 56;

> number
56

> number.ToString()
"56"

We've turned an int into a string type instead. Now it can be used in methods that work with strings.

Terminology


  • Arguments: Additional information provided to a method, passed in through the parentheses following the method name.

  • Substring: A small portion of a larger string. A bit like a single word in a larger sentence. For instance, "hello" is a substring of the string "hello world".

  • Pascal Case: Required naming convention for all C# methods in which all words are capitalized with no spaces. Examples include LikeThisMethodNameHere() or ThisOtherFictionalMethodHere().

Examples


Here are some examples of built-in methods you can call without arguments in the REPL:

> string phrase = "Programming is AWESOME";

> phrase
"Programming is AWESOME"

> phrase.ToUpper()
"PROGRAMMING IS AWESOME"

> phrase.ToLower()
"programming is awesome"

Calling built-in methods with provided arguments:

> string phraseOne = "hello world";
> string phraseTwo = "hello";

> phraseOne.Contains(phraseTwo)
true

Lesson 5 of 5
Last updated more than 3 months ago.