C# 101: Intro To C# Programming
C# 101: Intro To C# Programming
Introduction
• Your Name
• Your day job
• Your last holiday destination?
C# 101
• C# Fundamentals
– Setting up your development environment
– Language Overview
– How C# Works
– Writing your first program
– Built-in Data Types
– Conditionals and Loops
C# 102
• Object-oriented Programming
– Classes and Objects
– Polymorphism, Inheritance and Encapsulation
– Functions and Libraries
C# 103
• Data Structures
– Arrays
– Collections
C# 101: Introduction to C#
Language Overview
C# Language Overview
• Object-oriented
• Widely available
• Widely used
C# Versions
• Brief History…
- C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg, was previously
involved with the design of Pascal, Delphi and Visual J++
• Major Version Releases
– C# 1.0 (2002)
– C# 2.0 ( 2005)
– C# 3.0 (2008)
– C# 4.0 (2010)
– C# 5.0 (2012)
– C# 6.0 (2015)
Visual Studio Editions
• Visual Studio Standard Edition
• Visual Studio Enterprise Edition
• Visual Studio Community Edition
• Visual Studio Express Edition
.NET Framework
A programming infrastructure created by Microsoft for building,
deploying, and running applications and services that use .NET
technologies, such as desktop applications and Web services.
How C# works
How C# Works
C# File Structure
C# 101: Introduction to C#
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{ /* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
Compiling Your First Java Program
• Save the HelloWorld class in the IDE
• This automatically compiles the HelloWorld.cs file into
into a HelloWorld.class file
• Go to the folder you created the csharp101 project on
your hard disk and open the src folder.
• What do you see?
Running Your First C# Program
• Build and Run your program in Visual Studio
by Ctrl+Shift+B / F5
Anatomy of a C# Application
Comments Class Name Arguments
Access
modifier
Function/static
method
Language Features
Introduction to C#
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string x;
System.Console.WriteLine("Enter your name");
x = Console.ReadLine();
System.Console.WriteLine("Hi {0} , How are you", x);
Console.ReadKey();
}
}
}
Integer Data Type
• Useful for expressing algorithms.
Operator + - * /
Double Data Type
Expression Value
6.02e23 / 2 3.01e23
Math.sqrt(2.0) 1.4142135623730951
C# Math Library
Methods
Math.sin() Math.cos()
Math.log() Math.exp()
Math.sqrt() Math.pow()
Math.min() Math.max()
Math.abs() Math.PI
http://java.sun.com/javase/6/docs/api/java/lang/Math.html
Hands-on Exercise
Integer Operations
Exercise: Integer Operations
• Create a C# class named IntOps in the C101 project that performs integer
operations on a pair of integers from the command line and prints the results.
Solution: Integer Operations
int num1, num2, sum, prod, quot, rem;
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());
sum = num1 + num2;
prod = num1 * num2;
quot = num1 / num2;
rem = num1 % num2;
System.Console.WriteLine(" {0} : {1} = {2}", num1, num2,
sum.ToString());
System.Console.WriteLine(" {0} : {1} = {2}", num1, num2,
prod.ToString());
System.Console.WriteLine(" {0} : {1} = {2}", num1, num2,
quot.ToString());
System.Console.WriteLine(" {0} : {1} = {2}", num1, num2,
rem.ToString());
Console.ReadLine();
Boolean Data Type
• Useful to control logic and flow of a program.
a !a a b a && b a || b
Boolean isLeapYear;
Console.ReadLine();
Data Types Summary
• A data type is a set of values and operations on
those values.
– String for text processing
– double, int for mathematical calculation
– boolean for decision making
• Why do we need types?
– Type conversion must be done at some level.
– Compiler can help do it correctly.
– Example: in 1996, Ariane 5 rocket exploded after
takeoff because of bad type conversion.
Introduction to C#
if (Math.Sqrt(16) < 3)
System.Console.WriteLine("the number is less than 3");
Else
System.Console.WriteLine("the number is greater than 3");
Console.ReadLine();
More If Statement Examples
While Loop
• A common repetition structure.
– Evaluate a boolean expression.
– If true, execute some statements.
– Repeat.
For Loop
• Another common repetition structure.
– Execute initialization statement.
– Evaluate a boolean expression.
• If true, execute some statements.
– And then the increment statement.
– Repeat.
Anatomy of a For Loop
Console.ReadLine();
Hands-on Exercise
Powers of Two
Exercise: Powers of Two
• Create a new C# project in Visual Studio named Pow2
• Write a C# class named PowerOfTwo to print powers of 2 that are
<= 2N where N is a number passed as an argument to the program.
– Increment i from 0 to N.
– Double v each time
Solution: Power of 2
Control Flow Summary
• Sequence of statements that are actually
executed in a program.
• Conditionals and loops enable us to choreograph
the control flow.
Control flow Description Example
Homework Exercises
Hands-on Exercise
Array of Days
Exercise: Array of Days
• Create a C# class named DayPrinter that prints
out names of the days in a week from an array
using a for-loop.
Solution: Arrays of Days
Alex Johnson
23 Main Street
New York, NY 10001 USA
Further Reading