Edx C# Course
Edx C# Course
Pre-Course Survey
module 1
1. Introducing C#
Introduction
A History of C#
2. The Tools
Visual Studio
Xamarin
MonoDevelop
TextPad
Notepad
Statements
Identifiers
Operators
Self-Check Questions
4. Module Lab
Tutorial Lab
Self-Assessment Lab
module 2
1. Decision Statements
Lesson Introduction
The if Statement
Self-Check
Self-Check
2. Repetition in C#
Introducing Repetition
The do Loop
Self-Check
Self-Check
3. Module Lab
Self-Assessment Lab
Module 3
1. Introducing Editors
Module Introduction
2. Methods
Method Declarations
Calling Methods
Demo: Methods
Self-Check
Self-Check
3. Exception Handling
Exception Propagation
Handling Exceptions
Throwing Exceptions
Self-Check
Self-Check
4. Module Lab
Self-Assessment Lab
1. Post-Course Survey
For more information , you can see: Visual Studio: https://aka.ms/edx-dev204x-
vs01
MonoDevelop
MonoDevelop is another toolset that you can use to create
applications with C#. It is positioned as a cross platform IDE that
allows you to write desktop or web applications that can run on Linux,
Windows, and Mac OS X.
Multi-platform support
Code completion
Integrated debugger
Source Control
Unit Testing
Find it at http://www.monodevelop.com
TextPad
TextPad has been around for many years and is sometimes known as
The Text Editor for Windows. TextPad is essentially a plain text editor
that runs on Windows. It supports different programming and scripting
languages through add-ons known as clip libraries, dictionaries,
macros, and syntax definitions. When using TextPad to write C# code,
you'll be most interested in the Syntax Definitions that provide the
keyword color highlighting.
Choosing a tool like TextPad means that you have a lightweight editor
for your C# code files but it also means that you don't get the full
benefits of an IDE and as such you will be responsible for:
Notepad
Of course Microsoft Windows has the venerable Notepad app included
with every copy. You can write your code in Notepad and then use the
command-line compiler csc.exe to compile your code.
Take note that you will not realize any benefits such as code
completion, Intellisense, syntax highlighting, or complete project
solution and file management. And you will need to learn the available
options and switches for the command line compiler.
https://courses.edx.org/courses/course-
v1:Microsoft+DEV204.1x+4T2017/courseware/6ff2f1e658134f31bb8293
a370a26c73/31bd8f463a574601b7ff2ed9cf6d0127/?child=first
Data Types
Bookmarked
Data Types
All applications store and manipulate data within the computer's
memory. C# supports two kinds of data types used to represent real-
world information, value types and reference types.
Value types are so-called because they contain the actual value of the
data they store. For example, you might have an int type that stores
the value 3. The literal value of 3 is stored in the variable that you
declare to hold it.
With the exception of DateTime and string, in the below table,the data
types listed are aliases for structs in .NET that represent the data
types in the Microsoft .NET Framework. Anyplace you can use int you
can also use System.Int32. We'll cover structs in module four.
The following table shows the most commonly used value types.
Statements
Bookmarked
Statements
In C#, a statement is considered a command. Statements perform
some action in your code such as calling a method or performing
calculations. Statements are also used to declare variables and assign
values to them.
Example:
int myVariable = 2;
int
myVariable
=
2
;
int is the data type used for the variable called myVariable.
Identifiers
Bookmarked
Identifiers
In C#, an identifier is a name you give to the elements in your program.
Elements in your program include;
When you create a variable in C# you must give it a data type. The data
type tells the compiler and syntax checker what kind of information
you intend to store in that variable. If you try to assign data that is not
of that type, warnings or errors will inform you of this. This is part of
the type-safe nature of C#.
You can assign a value to the variable at the time you create it or later
in your program code. C# will not allow you to use an unassigned
variable to help prevent unwanted data from being used in your
application. The following code sample demonstrates declaring a
variable and assigning a value to it.
int myVar = 0;
C# has a set of reserved keywords that the language uses. You should
not use these keywords as an identifier in your code. You may choose
to take advantage of the case-sensitivity of C# and use Double as an
identifier to distinguish it from the reserved keyword double, but that
is not a recommended approach.
abstract As Base
Operators
Bookmarked
Operators
When writing C# code, you will often use operators. An operator is a
token that applies to operations on one or more operands in an
expression. An expression can be part of a statement, or the
entire statement. Examples include:
Not all operators are appropriate for all data types in C#. As an
example, in the preceding list the + operator was used to sum two
numbers. You can use the same operator to combine two strings into
one such as:
“Tom”++
Type
Arithmetic +, -, , /, %
String concatenation +
Casting ( ), as
Data Conversions
C# supports two inherent types of conversion (casting) for data types,
implicit and explicit. C# will use implicit conversion where it can,
mostly in the case when a conversion will not result in a loss of data
or when the conversion is possible with a compatible data type. The
following is an example of an implicit data conversion.
The long type has a 64-bit size in memory while the int type uses 32-
bits. Therefore, the long can easily accommodate any value stored in
the int type. Going from a long to an int may result in data loss
however and you should use explicit casting for that if you know what
data will be lost and it doesn't impact your code.
int myInt;
// Cast double to int by placing the type modifier ahead of the type to be
converted
// in parentheses
myInt = (int)myDouble;
int myInt;
// Cast double to int by using the Convert class and the ToInt32() method.
myInt = Convert.ToInt32(myDouble);
You will find many other methods in the Convert class that cast to
different integral data types such as ToBoolean(), ToByte(), ToChar(),
etc.
// TryParse() example
// Parse() example
8. Place your cursor immediately after the open curly brace in the Main
method, then press enter to create a new line
19.
28.
30.
32. Console.WriteLine(firstName);
33. Console.WriteLine(lastName);
34.
37.
Console.WriteLine($"Born on {birthDate}");
NOTE: It is highly recommended that you actually type in the code and
not simply copy and paste. Typing in the code allows you to evaluate
the syntax as you enter it and helps to reinforce your learning along
the way.
1. Press the CTRL+F5 keys to start the application without debugging, or,
using the menus, select Debug then Start Without Debugging.
2. This will cause Visual Studio to compile the code and the run the
application. A console window will open displaying the results if no errors are
encountered.
If you are not using Visual Studio, you will need to follow the
instructions for your specific IDE to create a new project or use the
compiler instructions to compile the C# code file that you
will created in your editor of choice.
The code you need to enter will depend on the editor you are using. For
example, if you are using a text editor, you will need to add the
surrounding structure in addition to above code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mod1_Lab1
class Program
int age = 0;
DateTime birthDate;
firstName = "Tom";
lastName = "Thumb";
age = 18;
country = "MyCountry";
Console.WriteLine(firstName);
Console.WriteLine(lastName);
Console.WriteLine($"Born on {birthDate}");
}
Self Assessment Lab
Bookmarked
Using this knowledge you will start to form some of the foundation for
a potential application. This assignment is designed to allow you to
focus on the data types that are appropriate for attributes of real-
world objects. Later in your C# studies, you will start to combine these
attributes with behaviors (methods) and then transition them into
object-oriented classes.
Also, this assignment offers suggestions for attributes for each object
but does not give explicit instructions on every field required. You will
be required to think through the attributes of these "objects" as you
create the necessary data to support them in an application. Your list
may be different in an application that you would create on your own.
You can use the different listed objects for more practice if you want.
Student Information
First Name Last Name Birthdate Address Line 1 Address Line 2 City St
Teacher Information
First Name Last Name Birthdate Address Line 1 Address Line 2 City St
UProgram Information
Degree Information
Course Information
Introduction
C# decision structures provide logic in your application code that
allows the execution of different sections of code depending on the
state of data in the application. You might ask users whether they wish
to save any changes to a file that is open in the application. The
decision structure permits you to code behavior to execute based on
the answer provided by the user. Visual C# uses conditional
statements to achieve this functionality.
The if Statement
Bookmark this page
C# if Statements
In C#, if statements are concerned with Boolean logic. If the statement
is true, the block of code associated with the if statement is executed.
If the statement is false, control either falls through to the line after
the if statement, or after the closing curly brace of an if statement
block.
if (response == "Yes")
Note the use of curly braces in the code sample. You can eliminate the
curly braces if your statement to execute is a single line statement. C#
understands that if no curly braces are used, the line immediately
after the if(condition) will be executed if the condition is true.
Otherwise, it is not. To avoid confusion as to which lines will execute
for a true condition, a recommended practice is to always use curly
braces for your if statement.
In C#, if statements can also have associated else clauses. The else
clause executes when the if statement is false.
string response;
if (response == "connection_failed")
{
// Block of code executes if the value of the response variable is
"connection_failed".
else
//else if Statements
string response;
if (response == "connection_failed")
{
// Block of code executes if the value of the response variable is
"connection_error".
else
You can create as many else if blocks as necessary for your logic, or
until you become completely lost from too many else if clauses. If you
require any more than five else if clauses, you might want to consider
the switch statement, presented next.
The following sample shows how you can use a switch statement to
replace a collection of else if clauses.
//switch Statement
string response;
switch (response)
case "connection_failed":
break;
case "connection_success":
break;
case "connection_error":
break;
default:
break;
Notice that there is a block labeled default:. This block of code will
execute when none of the other blocks match.
In each case statement, notice the break keyword. This causes control
to jump to the end of the switch after processing the block of code. If
you omit the break keyword, your code will not compile. If you want to
handle multiple cases with the same code segment, you can use a fall-
through setup similar to this sample code.
string response;
switch (response)
case "connection_success":
break;
case "connection_failed":
case "connection_error":
// or "connection_error;
break;
default:
break;
sbyte
byte
short
ushort
int
uint
long
ulong
char
string
enumerations
Introducing Repetition
Bookmark this page
Introducing Repetition
Repetition is essentially the concept of doing something in a repeating
manner. In programming, you will typically use repetition to iterate
over items in a collection or to perform the same task over and over
again to produce the desired effect in your program.
The following code example shows how to use a for loop to execute a
code block 10 times.
//for Loop
// Code to execute.
The following code example shows how to use a foreach loop to iterate
a string array.
//foreach Loop
// Code to execute.
C# handles determining how many items are in the array and will stop
executing the loop when the end is reached. The
use of foreach loops can help prevent index out of bounds errors on
arrays.
//while Loop
response = PromptUser();