0% found this document useful (0 votes)
29 views62 pages

C# Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views62 pages

C# Fundamentals

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

C# Fundamentals

Quenneth Kyle Surban


Microsoft Student Partner
Why use the C# language?
What can you do???


Develop UNIVERSAL Develop Use it for And so
Windows apps websites cross-platform app much more
development
Overview
• Basic program structure
• Common Data Types and Variables
• Conditional Statements
• Loops
• Classes, Properties and Methods
• Collections
• Language Integrated Query (LInQ)
Basic Program Structure

Visual Studio
Demo: Hello World!

Visual Studio
Hello World!
using System;

namespace HelloWorldDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Hello World!
using System;

namespace HelloWorldDemo Namespace


{ • This provides a way to organize
class Program related classes and other types
{
static void Main(string[] args) • Consider it as a folder for all of
{ your classes
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Hello World!
using System;

namespace HelloWorldDemo Classes


{ • This is a template for objects
class Program • Normally, it contains your properties
{
and method
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Hello World!
using System;

namespace HelloWorldDemo Main() method


{ • This is first method that the
class Program application runs (for Console
{
Applications)
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Hello World!
using System;

namespace HelloWorldDemo using Directive


{ • This calls libraries that your application
class Program will use
• Call libraries using their namespace
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
Data Types and Variables

Visual Studio
Common Data Types
• int – -1, 0, 3, 100, 1000, -500
• long – -50000000, 80000000,
• float – -5.76, -3.666, 8.45, 10.0
• double – -783.345793, 384.94928
• char – A, 5, !
• string – Hello World
• bool – true, false, 1, 0
Variables
• Variables are used to hold a value (Just like Math)
• In programming, variables hold a specific type of data
• Syntax:
[dataType] [variableName] = [value Optional];
• Access data by using the variable name
Arrays
• Arrays are “special variables” that can hold a set of data
• Syntax:
[dataType][] [variableName] = new [datatype] [Size];
• Example:
int[] numbers=new int[5];
int[] numbers=new int[5]{1,2,3,4,5}
• Access the data by using the variable name and index (position).

Note: The first index starts with 0 not 1.


Demo: Variables and Arrays

Visual Studio
Conditional Statements

Visual Studio
Conditional Statements
• Computer instructions are executed sequentially (by default)
• Conditional statements allows select instructions to run based
on the comparison of two or more data
• In the most basic sense, it involves a YES/NO Question
• Two Types of Conditional Statements
• If – Else Statement
• Switch Statement
If…Else… Statements
Syntax: If… Else… checks the condition and
if(condition) runs the code that follows if its
true. Otherwise, it runs the other
{ one.
Instructions
} Conditions (YES/NO Question)
Else returns a Boolean value. It can
come from:
{ • Comparator and Logical
Instructions Operators
} • Boolean variables
If…Else… Statements
Syntax: Comparators
if(condition) • Equal (==)
{ • Greater than (>)
Instructions • Less than (<)
} • Not Equal (!=)
Else Logical Operators
{ • And (&&)
Instructions
• Or (||)
}
If Else - How it works
if(1>0)
{
Console.WriteLine(“True”);
}
else
{
Console.WriteLine(“False”);
}
If Else - How it works
if(-1>0)
{
Console.WriteLine(“True”);
}
else
{
Console.WriteLine(“False”);
}
Switch Statements
Syntax: The switch statement…
switch(variable)
• Compares the value of the
{
variable to each case values
case [value1]:
instruction • When both values are the
break; same, it runs the code that
case [value2]: follows
instruction • The break is used to jump out
break; of the switch statement

}
Switch - How it works
switch(1)
{
Output:
case 0:
WriteLine(“Zero”);
break;
case 1:
One
WriteLine(“One”);
break;
case 2:
WriteLine(“Two”);
break;
}
Switch - How it works
switch(1)
{ Be sure to use break. Otherwise,
case 0:
instructions will run sequentially
WriteLine(“Zero”);
break;
case 1:
WriteLine(“One”); Output:
case 2:
WriteLine(“Two”); One
break;
} Two
Demo: Control Statements

Visual Studio
Loops

Visual Studio
Loops
• In this context, to loop is to 4 Loop Statements
repeat • For Loop
• It allows the repetition of a • While Loop
set of instructions several
times • Do…While Loop
• For each Loop
For Loop
• A loop whereby you check whether a particular condition is true
before you repeat the set of instructions
• Syntax:
for (initializer; condition; iterator) { instructions }

Where:
The initializer is a local variable to serve as a loop counter
The condition is what determines how many times the instruction is
repeated
The iterator is what happens to the loop counter after executing the
set of instructions
For Loop
Scenario: Code:
Display the following:
Console.WriteLine(1);
1 Console.WriteLine(2);
2 Console.WriteLine(3);
3 Console.WriteLine(4);
4 Console.WriteLine(5);
5
For Loop
Another Solution: Output:

for(int counter=1; 1
counter<6; 2
counter++) 3
{ 4
Console.WriteLine(counter) 5
}
While Loop
• This is a loop that first checks if a condition is met. If its true, it
will repeat the instruction until it becomes false. Otherwise, it
will skip the instructions.
• Consider it a repeating if statement
• Syntax
while(condition)
{
statement;
}
While Loop
Output: int number=1;
while(number<6)
1 {
2 Console.WriteLine(number);
3 number++;
4 }
5
Do…While Loop
• The do…while loop executes the set of instructions once and
then repeats if the condition is met.
• Syntax
do
{
statement
}
while(condition)
Do…While Loop
Output: int number=1;
do
1 {
2 Console.WriteLine(number);
3 number++;
4 }
5 while(number<6);
While VS Do…While

While Loop Do…While Loop


int number=1; int number=1;
while(number<1) do
{ {
Console.WriteLine(number);
Console.WriteLine(number);
number++;
number++;
}
} while(number<1);

Output: Output: 1
For Each Loop
• Mainly used for looping through Arrays and Collections
• The number of loops it does is based on the number of items
on the array or collection
• Syntax
foreach([dataType] [variableName] in [variableNameOfArray/Collection])
{
instruction
}
For Each Loop
int[] list=new int[5]{1,4,9,34,12); Output

foreach(int number in list) 1


{ 4
Console.WriteLine(number); 9
} 34
12
Demo: Loops

Visual Studio
Classes

Visual Studio
Objects
• What are objects?
• In real life, it is a tangible and
visible entity
• In computing, objects are
virtual items or entity

• Objects are characterized by:


• Data that defines it (Properties)
• Methods of operation
(Methods)
Classes
• This is the blueprint or
template of any objects.
• It defines properties and
methods that is applicable to
an object
Members of a Class: Properties
• This is a data member
• It is variable that is always present in the object
• This typically holds characteristics of an object
• Syntax:
Read/Write: public [dataType] [propertyName] { get; set;}
ReadOnly: public [dataType] [propertyName] { get; }
WriteOnly: public [dataType] [propertyName] { get; set;}
Member of a Class: Methods
• This is a function member
• This is a set of instructions for manipulating data
• Syntax:
public [returnType] [methodName] ([parameters Optional])
{instructions}

The returnType is determines data type of the result (Use void if no result
is needed)
The methodName is the name used to call the method
The parameters are data needed to perform the operation. They are
defined by [dataType] [parameterName].
Characteristics??? SKILLS???
Ideal Basketball Player
• Characteristics • Skills
 Height  Dribbling
 Weight  Shooting
 Name  Passing
 Age
Demo: Create a Class

Visual Studio
Creating an Object
• Classes are just templates
• To create an object, you need to specify which template(class) it
will use in order for it to be built (instantiated).
• Two Ways to Instantiate an Object
• Default Constructor – create an object with blank properties
[className] [objectName] = new [className]();
• Object Initializer – create an object with properties
[className] [objectName] = new [className]()
{ [propety1 = value1], [property2 = value2], …};
Demo:
Creating and Using an Object

Visual Studio
Collections

Visual Studio
Collections
• Collections provide a more flexible way to work with
groups/sets of objects
• A collection is a class, so you must declare an instance of the
class before you can add elements to that collection
• Commonly Used Collections
• List<T>
• Queue<T> (First IN, First OUT)
• Stack <T> (Last IN, First OUT)

Note: T must be substituted with a data type or class


Collection VS Array

Collection Array
• Dynamic • Fixed
• The number of data that it • The number of data it can
can hold is infinite hold is fixed during its
instantiation.
List Collection Initializer
• To immediately populate your Collection with data, use
Collection Initializers
• Syntax:
List<T> [variableName] = new List<T>()
{
[Object Initializer], [Object Initializer], …
};
Demo: List Collection

Visual Studio
Language Integrated Query
(LINQ)

Visual Studio
Language Integrated Query
• LINQ (Language Integrated Query) integrates query syntax
inside the C# programming language, making it possible to
access different data sources with the same syntax.
• Common Query Clauses
• From
• Where
• OrderBy
• Descending
• Select
Using Language Integrated Query
Example:

var USRacers = from racerList in getRacers()


where racerList.Country = “US”
orderby racerList.LastName descending
select racerList
Using LINQ Extension Methods
Example

List<Racer> USRacer=getRacers()
.Where(racerList => racerList.Country==“US)
.OrderByDescending(racerList => racerList.LastName)
.ToList();
Demo: LINQ

Visual Studio
What we’ve discussed
• Basic program structure
• Common Data Types and Variables
• Conditional Statements
• Loops
• Classes, Properties and Methods
• Collections
• Language Integrated Query (LInQ)
Resources
• C# Programming Guide
https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx
http://bit.ly/1LWeKsO
• C# Fundamental for Absolute Beginners
http://bit.ly/1HA4YdZ

• Microsoft Developer Network – http://msdn.microsoft.com


• Windows Dev Center – http://dev.windows.com
• Microsoft Virtual Academy – http://aka.ms/mva
C# Fundamentals
Quenneth Kyle Surban
Microsoft Student Partner
Quenneth.Surban@studentpartner.com

You might also like