Last active
February 17, 2021 03:15
-
-
Save MbuguaCaleb/4a6ee2209a317b849087bb467e94cfe2 to your computer and use it in GitHub Desktop.
Fundamentals of the C# Programming Language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Strings and String Methods. | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//we put all the code we want our program to execute at the Main method | |
string name= "Caleb is Born Again!"; | |
//length of the string returned. | |
Console.WriteLine(name.Length); | |
Console.WriteLine(name.ToUpper()); | |
Console.WriteLine(name.ToLower()); | |
Console.WriteLine(name.Contains("Born")); | |
Console.WriteLine(name[0]); | |
Console.WriteLine(name.IndexOf('C')); | |
Console.WriteLine(name.Substring(9)); | |
Console.WriteLine(name.Substring(9, 3)); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Numbers | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//we put all the code we want our program to execute at the Main method | |
int num = 6; | |
//Console.WriteLine(num++); | |
Console.WriteLine(num); | |
Console.WriteLine(++num); | |
//Numbers Methods | |
Console.WriteLine(Math.Abs(-30)); | |
Console.WriteLine(Math.Pow(4.5, 2)); | |
Console.WriteLine(Math.Sqrt(36)); | |
Console.WriteLine(Math.Max(4, 99)); | |
Console.WriteLine(Math.Round(4.7)); | |
//Console.WriteLine(5 * 5); | |
//Console.WriteLine(5 % 2); | |
//Console.WriteLine(4+2*3); | |
//Console.WriteLine((4+2)*3); | |
//Integer Operations always return integers | |
//Console.WriteLine(5 / 2); | |
//This would be the correct way for the above | |
//Console.WriteLine(5 / 2.0); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Getting User Input | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//write does not progress our cursor to the next page | |
Console.Write("Hello, What is your name ? "); | |
string name = Console.ReadLine(); | |
Console.Write("Hello, What is your age ? "); | |
string age = Console.ReadLine(); | |
Console.WriteLine("Congrats! Your name is" + name +"You are "+ age + "Years Old" ); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Building a Basic Calculator | |
##Convert String to an Integer | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Converting a String to a Number | |
int a = Convert.ToInt32("45"); | |
Console.WriteLine(a + 5); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##The Calculator | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Converting a String to a Number | |
//C# is very sensitive | |
//If you declare an integer as your type there is no way that a double will work. | |
Console.Write("Enter a number: "); | |
double a = Convert.ToDouble(Console.ReadLine()); | |
Console.Write("Enter another number"); | |
double b = Convert.ToDouble(Console.ReadLine()); | |
Console.WriteLine(a + b); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Simple MadLips Game | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string color, pluralNoun, celebrity; | |
Console.Write("Enter a Color: "); | |
color = Console.ReadLine(); | |
Console.Write("Enter a pluralNoun:"); | |
pluralNoun = Console.ReadLine(); | |
Console.Write("Enter a Celebrity:"); | |
celebrity = Console.ReadLine(); | |
Console.WriteLine("Roses are" + color); | |
Console.WriteLine(pluralNoun+ "are Blue"); | |
Console.WriteLine("I love " + celebrity); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Arrays | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Array is a data structure that enables us to hold mutiple pieces of information in a container | |
int[] luckyNumbers = { 1,2,3,4,6,7,8,9,3}; | |
luckyNumbers[1] = 20; | |
//All the pieces of information need to be of the same type | |
//When i am makig a dynamic array where i dont know exacly what values will be there | |
//ido as below | |
//i must tell c# what the size of my array will be | |
string[] friends = new string[5]; | |
friends[0] = "Justin"; | |
friends[1] = "Brian"; | |
Console.WriteLine(luckyNumbers[1]); | |
Console.WriteLine(friends[1]); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Methods Intro | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//notice that am calling my methods from the main function. | |
SayHi(); | |
Response("Good",10); | |
Response("Better",15); | |
Response("Best",20); | |
Console.ReadLine(); | |
} | |
static void SayHi() | |
{ | |
//Only characters in C# can be enclosed in single quotes but not strings | |
Console.WriteLine("Caleb Says Hi!"); | |
} | |
static void Response(string response,int age) | |
{ | |
Console.WriteLine("I am doing" + response+ "I am "+ age + "Years Old"); | |
} | |
} | |
} | |
##Methods Return Statement | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int cubedTwo = cube(2); | |
Console.WriteLine(cubedTwo); | |
Console.WriteLine(cube(20)); | |
Console.ReadLine(); | |
} | |
static int cube(int num) | |
{ | |
int result = num * num * num; | |
return result; | |
} | |
} | |
} | |
##Beginning if Statements | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
bool isMale = true; | |
bool isTall = true; | |
//Both Conditions have to be True | |
if (isMale && isTall) | |
{ | |
Console.WriteLine("You are a Tall Male"); | |
} | |
if(!isMale && isTall) | |
{ | |
Console.WriteLine("You are not male but you are tall"); | |
} | |
else if(isMale && !isTall) | |
{ | |
Console.WriteLine("You are Male and you are Not tall"); | |
} | |
else | |
{ | |
Console.WriteLine("You are eith not male or tall or both"); | |
} | |
//Only one condition has to be True | |
if (isMale || isTall) | |
{ | |
Console.WriteLine("You are a Tall Male"); | |
} | |
else | |
{ | |
Console.WriteLine("You are eith not male or tall or both"); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Comparison Operators | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int max = GetMax(11, 10,223); | |
Console.WriteLine(max); | |
Console.ReadLine(); | |
} | |
static int GetMax(int num1 ,int num2,int num3) | |
{ | |
int result; | |
/* | |
Comparison operators | |
= (Assignemt) | |
== (Comparison) | |
===(Also checks the Type) | |
!= (Evalates not Equal to) | |
> Greater than < Less than | |
>= | |
<= | |
*/ | |
if (num1>=num2 && num1>=num3) | |
{ | |
result = num1; | |
} | |
else if(num2>=num1 && num2 >= num3) | |
{ | |
result = num2; | |
} | |
else | |
{ | |
result = num3; | |
} | |
return result; | |
} | |
} | |
} | |
##More Complex Calculator | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter a Number:"); | |
double num1 = Convert.ToDouble(Console.ReadLine()); | |
Console.Write("Enter an Operator"); | |
string op = Console.ReadLine(); | |
Console.Write("Enter a Second Number:"); | |
double num2 = Convert.ToDouble(Console.ReadLine()); | |
if( op == "+") | |
{ | |
Console.WriteLine(num1 + num2); | |
} | |
else if (op == "-") | |
{ | |
Console.WriteLine(num1 - num2); | |
} | |
else if (op == "/") | |
{ | |
Console.WriteLine(num1 / num2); | |
} | |
else if (op == "*") | |
{ | |
Console.WriteLine(num1 * num2); | |
} | |
else | |
{ | |
Console.WriteLine("Invalid Operand!!!"); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Switch Statements | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("kindly key in the Day:"); | |
int day = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine(GetDay(day)); | |
Console.ReadLine(); | |
} | |
//functions must always be executed outside of my main | |
//A switch case is a special type of if statement that can evaluate Mutiple Conditions | |
static string GetDay(int dayNum) | |
{ | |
string dayName; | |
switch (dayNum) | |
{ | |
case 0: | |
dayName = "Sunday"; | |
break; | |
case 1: | |
dayName = "Monday"; | |
break; | |
case 2: | |
dayName = "Tuesday"; | |
break; | |
case 3: | |
dayName = "Wednesday"; | |
break; | |
case 4: | |
dayName = "Thursday"; | |
break; | |
case 5: | |
dayName = "Friday"; | |
break; | |
case 6: | |
dayName = "Saturday"; | |
break; | |
default: | |
dayName = "Invalid Day provided"; | |
break; | |
} | |
return dayName; | |
} | |
} | |
} | |
##While and Do While Loops | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Loops over a certain condition as long as it is True | |
int index = 1; | |
while(index <= 5) | |
{ | |
Console.WriteLine(index); | |
index++; | |
} | |
//DO While loop | |
//It executes the code in the loop at least once before checking the condition | |
int indexTwo = 6; | |
do | |
{ | |
Console.WriteLine(indexTwo); | |
indexTwo++; | |
} while (indexTwo <= 5); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Loops Game; | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Loops over a certain condition as long as it is True | |
int index = 1; | |
while(index <= 5) | |
{ | |
Console.WriteLine(index); | |
index++; | |
} | |
//DO While loop | |
//It executes the code in the loop at least once before checking the condition | |
int indexTwo = 6; | |
do | |
{ | |
Console.WriteLine(indexTwo); | |
indexTwo++; | |
} while (indexTwo <= 5); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##While and Do While Loop Game | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string secretWord = "Caleb"; | |
string guess = ""; | |
/* | |
while (guess != secretWord) | |
{ | |
Console.WriteLine("Enter the Secret Word!"); | |
guess = Console.ReadLine(); | |
} | |
Console.WriteLine("You Win!!"); | |
*/ | |
//Using Do while | |
do | |
{ | |
Console.WriteLine("Enter the Secret Word!"); | |
guess = Console.ReadLine(); | |
} while (guess != secretWord); | |
Console.WriteLine("Cograts!! You win"); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Advanced While Loop Game | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string secretWord = "Caleb"; | |
string guess = ""; | |
int guessCount =0; | |
int guessLimit = 3; | |
bool limitsExhasted = false; | |
while (guess != secretWord && !limitsExhasted) | |
{ | |
if(guessCount < guessLimit) | |
{ | |
Console.WriteLine("Enter the Secret Word!"); | |
guess = Console.ReadLine(); | |
guessCount++; | |
} | |
else | |
{ | |
limitsExhasted = true; | |
} | |
} | |
if (limitsExhasted) | |
{ | |
Console.WriteLine("Ooops!Your chances got exhasisted!!"); | |
} | |
else | |
{ | |
Console.WriteLine("You Win!!"); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
##For Loop | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
for(int i=0; i<=5; i++) | |
{ | |
Console.WriteLine(i); | |
} | |
int [] gradesArray = { 20,21,93,33,55,66,67}; | |
for(int i =0; i<gradesArray.Length; i++) | |
{ | |
Console.WriteLine(gradesArray[i]); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Exponential For Loop | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(PowerLoop(3, 5)); | |
Console.ReadLine(); | |
} | |
//remeber functions convetions | |
static int PowerLoop(int baseNum, int powerNum) | |
{ | |
int result = 1; | |
for( int i =0; i< powerNum; i++ ) | |
{ | |
result = result * baseNum; | |
} | |
return result; | |
} | |
} | |
} | |
##2 Dimensional Arrays | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Declaration | |
int[,] twoDimensionalArray = | |
{ | |
{1,2 }, | |
{3,4 }, | |
{5,6 }, | |
{7,8 } | |
}; | |
//Dymanic declaration | |
int[,] dynamicDeclaration = new int[2, 3]; | |
Console.WriteLine(twoDimensionalArray[1, 1]); | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Comments | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//single line comments | |
/* | |
* Multi Line Comments. | |
* | |
*/ | |
Console.ReadLine(); | |
} | |
} | |
} | |
##Execption Handling | |
1.General Try Catch Exception | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Console.Write("Enter a Number:"); | |
int num1 = Convert.ToInt32(Console.ReadLine()); | |
Console.Write("Enter a Second Number:"); | |
int num2 = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine(num1 / num2); | |
} | |
catch(Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
2.More Ellaborate Exceptions | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Console.Write("Enter a Number:"); | |
int num1 = Convert.ToInt32(Console.ReadLine()); | |
Console.Write("Enter a Second Number:"); | |
int num2 = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine(num1 / num2); | |
} | |
catch(DivideByZeroException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
catch(FormatException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
3.Adding finally that is going to Execute no matter what. | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
Console.Write("Enter a Number:"); | |
int num1 = Convert.ToInt32(Console.ReadLine()); | |
Console.Write("Enter a Second Number:"); | |
int num2 = Convert.ToInt32(Console.ReadLine()); | |
Console.WriteLine(num1 / num2); | |
} | |
catch(DivideByZeroException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
catch(FormatException e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
finally | |
{ | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
4.Classes Constructor Method. | |
##Class | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
namespace csharp_one | |
{ | |
//Classes are Le New Datatypes which we create and which Model Real World Entities. | |
class Book | |
{ | |
//class attributes.They define the blue print of my Classes. | |
//Classes gives us more of the templates of an object | |
public string title; | |
public string author; | |
public int pages; | |
//Constuctor is a Method that gets called when we create an object of that class | |
//It is the Method that is given the name of the class | |
public Book(string aTitle, string aAuthor,int aPages) | |
{ | |
title = aTitle; | |
author = aAuthor; | |
pages = aPages; | |
} | |
} | |
} | |
##Calling Class | |
using System; | |
namespace csharp_one | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Book book1 = new Book("Deutronomy", "Moses", 200); | |
Book book2= new Book("Genesis", "Moses", 200); | |
//Updating class Attributes. | |
book2.title = "Leviticus"; | |
Console.WriteLine(book2.title); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment