title: “Exploring Built-In Functions” —
The C# language actually has a lot of built-in functionality in the form of classes and functions. We’ll talk more about Classes in the next lesson.
But let’s explore some of the built-in functions that are available to us!
There are lots of interesting things you can do with strings, such as analyzing user input text.
Go find the C# documenation for strings, and learn about the following string functions:
For instance, you could write a program that tells you whether an input string contains the word “banana”;
string userInput = Console.ReadLine();
bool containsYes = userInput.Contains("banana");
Console.WriteLine(" Contains the word 'banana': " + containsYes);
Here’s a bit of code that will open a browser window to a webpage the user specifies:
Console.WriteLine("What webpage would you like to visit? Be sure to put http:// at the beginning!");
string webPage = Console.ReadLine();
System.Diagnostics.Process.Start("explorer", webPage);
Here’s a bit of code that will load the contents of a text file from a particular location on the hard drive, and print the contents to the console:
//read all the contents of a file
//NOTE: (You need to go to the c:\Users\Public\ folder in Windows Explorder, make a new folder called "Test", and then make a new text file there called test.txt)
//alternatively, you could make this program ask the user for a path
string contents = System.IO.File.ReadAllText("C:\\Users\\Public\\Test\\text.txt");
Console.WriteLine("File contents: " + contents);
You can use File.ReadAllLines to read all of the text out of a file.
C# comes with a whole library of math functions called MathF. For instance, here’s a function to compute the area of a circle given its radius, using the MathF.PI variable:
float ComputeCircleArea(float radius)
{
return MathF.PI * (radius * radius);
}
Not sure how to do a particular thing in code? Chances are, there are samples out on the internet! One of the most important skills you’ll develop as a programmer is figuring out how to find existing solutions for problems.
Google is your friend!