Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Welcome to C#

No description
by

David Atkinson-Beaumont

on 30 September 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Welcome to C#

Welcome to C#
Sample Program 1
Rock - Paper - Scissors - Lizard - Spock
What is C#?
Modern, general purpose programming language
Object oriented
Easy to learn
Structured language
It produces efficient programs
It can be compiled on a variety of computer platforms
Part of .Net Framework
Getting Started
We will be writing C# code using the MonoDevelop IDE which comes as standard with Unity on Windows
General Rules of C#
C# is case sensitive.
All statements and expression must end with a semicolon (;).
The program execution starts at the Main method.
Unlike Java, file name could be different from the class name.
R/P/S
Your first sample program will be a console application which will allow players to try and beat the computer in a game of Rock, Paper, Scissors, Lizard, Spock (RPSLS). Your game will have a text interface and consideration should be given to HCI
This screen shows a simple C#
program which displays text in
a console application
Understanding the code:
Line 1: The using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
Line 3 has the namespace declaration. A namespace is a collection of classes. rockPaperScissors namespace contains the class MainClass.
Line 5 has a class declaration, the class MainClass, contains the data and method definitions that your program uses. Classes generally would contain more than one method. Methods define the behavior of the class. However, the MainClass class has only one method called Main.
Line 7 defines the Main method, which is the entry point for all C# programs. The Main method states what the class will do when executed
The Main method specifies its behavior with the statement Console.WriteLine("Welcome to..."); WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Welcome to ..." to be displayed on the screen.
When we add the following code on a new line under line 9
Console.ReadKey();
What is different? How is this better?
Now we add this line after line 10 but still within the Main method
/* my first program in C# */
What is different? What does this line do?
How have we ensured that these three lines (9-11) are part of the Main method?
C# Basics
C# is Object Oriented which means is uses Objects which interact with each other by means of Methods.
Objects are created by a class
Objects can have attributes
Consider the following code:
using System;

namespace RectangleApplication
{
class Rectangle
{
// member variables
double length;
double width;

public void Acceptdetails()
{
length = 4.5;
width = 3.5;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails(); r.Display(); Console.ReadLine();
}
}
}
Variables are attributes or data members of a class, used for storing data.
In the preceding program, the Rectangle class has two member variables named length and width.
Member Variables
Member Functions
Functions are set of statements that perform a specific task. The member functions of a class are declared within the class. Our sample class Rectangle contains three member functions:AcceptDetails, GetArea and Display.
Instantiating a Class
In the preceding program, the class ExecuteRectangle is used as a class which contains the Main()method and instantiates the Rectangle class.
Returning to our original Class lets change the MainClass code
static string playerName;
public static void Main(string [] args)
{
Console.WriteLine("Welcome to Rock/Paper/Scissors\n");
Console.WriteLine("What is your name?");
playerName = Console.ReadLine();
Console.WriteLine("Hello " + playerName + ", I accept your challenge");
}
1: Explain the top line
2: What is the purpose of the main method?
3: On which line is the variable being populated?
4: What does \n mean?
Lets change how the class gets started
Variables
A variable is nothing but a name given to a storage area that our programs can manipulate.
Each variable in C# has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Variables can be changed, converted and emptied, check out these examples
int d = 3, f = 5; /* initializing d and f. */

byte z = 22; /* initializes z. */

double pi = 3.14159; /* declares an approximation of pi. */

char x = 'x'; /* the variable x has the value 'x'. */
int num;

num = Convert.ToInt32(Console.ReadLIne());
Whats this last one?
What does static mean?
it means that the variable is stored in memory so it can be used whenever you need it
What was static in the code?
Why do this?
public static void playGame(){
Random randomNumber = new Random();
string[] choices = new string[] { "Rock", "Paper", "Scissors" };
Console.WriteLine ("Please choose Rock(1), Paper(2) or Scissors(3)\n");
try{
playerChoice = Convert.ToInt32(Console.ReadLine());
playerChoice--;
}catch(FormatException e){
Console.WriteLine ("That was not the correct response, try pressing 1,2 or 3\n");
playGame();
}
next we add another class - playGame()
Theres a lot of new and important stuff here
First of all is the Random Object
This is an object that is included as part of the System library that allows us to generate random numbers
The next newbie is the String Array
string[] choices = new string[] { "Rock", "Paper", "Scissors" };
An array is a collection of multiple values. They have an index that lets you get to the values contained within and its a simple numbered index - like houses on a street
BUT
The counting starts at 0
The next new code is the Exception Handling
We use a Try/Catch block to anticipate when an error could be caused - in this case it would be if the user types in anything but 1,2 or 3
We put the code that might cause the error in the Try brackets and the code that deals with an error in the catch block
The exception that we expect to catch should be a FormatException so we specify this in the catch block. If we didn't then it would catch all exceptions and we wouldn't know if any other errors were occuring
Next the computer gets to choose a number between 1 and 3 using the random object we created earlier.
Why are we using 1-3 all the time?
Then we have this line:
Try and explain that one
Then we have to figure out who won
Why the last line?
Full transcript