0% found this document useful (0 votes)
22 views

Programmation Csharp2

The document provides an overview of prerequisites and basics of Windows programming using Csharp language. It discusses topics like prerequisites for development, Csharp syntax, data types, variables, operators, arrays and more.

Uploaded by

Ines
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Programmation Csharp2

The document provides an overview of prerequisites and basics of Windows programming using Csharp language. It discusses topics like prerequisites for development, Csharp syntax, data types, variables, operators, arrays and more.

Uploaded by

Ines
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 65

Windows programming

Willy-joël TCHANA
Software engineer : Software Architect
CEO of Over Soft Solution

1 @willytchana @osschannel +237 677 392 752


2 Agenda

 Prerequisites
 Basics of Csharp language
3 Prerequisites

 Computer Core i5, 8 Gb RAM recommended


 Windows OS 10 last update
 The dotnet Framework 4.8
 IDE Visual studio latest
 Platform programming language
Prerequisites
4
Computer Core i5, 8 Gb RAM recommended
Prerequisites
5 Computer Core i5, 8 Gb RAM recommended
Prerequisites
6 Windows OS 10 last update

https://go.microsoft.com/fwlink/?LinkID=799445
Prerequisites
7 The dotnet Framework 4.8

 A Framework
 .Net Framework 4x
 .Net Core 3x
 .Net 5
 A runtime
8
Prerequisites
IDE Visual Studio latest

 VS Installer
 h
tps://visualstudio.microsoft.com/fr/thank-you-downloading-visual-studio/?sku
=Community&rel=16

 VS 2019 Community
 Outlook account
Prerequisites
9
IDE Visual Studio latest: Visual studio installer
Prerequisites
10
IDE Visual Studio latest: VS installer
Prerequisites
11
IDE Visual Studio latest: VS 2019 community
Prerequisites
12
IDE Visual Studio latest: VS 2019 with account
Prerequisites
13 Platform programming language

 VB.NET
 C++
 F#
 C#
 All this language are oriented programming language
Prerequisites
14 Platform programming language
Basics of Csharp language
15
 Syntax
 Base data types
 Variable declaration
 Assignment
 Cast
 Input / Output (I/O)*
 Operators
 Conditional instructions
 Iterative instructions
 Arrays
 Functions
 Nullable types*
 Data structures
 Tuple & ValueTuple*
 Conventions
Basics of Csharp language
16 Syntax

using System;
namespace MyCSharpProject
{
class Program
{
/*Like C program,
C# start with execution with main function
*/
static void Main(string[] args)
{
string message = "Hello World!!";
//Print to screen
Console.WriteLine(message);
}
}
}
Basics of Csharp language
17 Base data types: Numbers
Basics of Csharp language
18 Base data types: Strings

 char: 'a', '1', '+'


 string: "this is a string", "s"
 Special Characters
 \
 \n, \t, \r, etc.
 Escape Sequence : @
 Ex: @"xyzdefr\abc";
 String concatenation
 Ex: "Mr." + firstName + " " + lastName + ", Code: " + code;
 String Interpolation : $
 $"Mr. {firstName} {lastName}, Code: {code}";
Basics of Csharp language
19 Base data types: Boolean

 bool: true, false


Basics of Csharp language
20 Base data types: DateTime

 DateTime is not a C# data type, it’s a .Net


Framework type
 You need to import namespace System
 ISO Format: yyyy-MM-dd
Basics of Csharp language
21 Base data types: enum

 An enum (or enumeration type) is used to assign constant names to a group of numeric integer values.
 An enum is defined using the enum keyword, directly inside a namespace, class, or structure
enum WeekDays
{
Monday, // 0
Tuesday, // 1
Wednesday, // 2
Thursday, // 3
Friday, // 4
Saturday, // 5
Sunday // 6
}
Basics of Csharp language
22 Base data types: Assign values to enum members

enum Categories
{
Electronics, // 0
Food, // 1
Automotive = 6, // 6
Arts, // 7
BeautyCare, // 8
Fashion // 9
}
Basics of Csharp language
23 Base data types: Access enum

Console.WriteLine(WeekDays.Monday); // Monday
Console.WriteLine(WeekDays.Tuesday); // Tuesday
Console.WriteLine(WeekDays.Wednesday); // Wednesday
Console.WriteLine(WeekDays.Thursday); // Thursday
Console.WriteLine(WeekDays.Friday); // Friday
Console.WriteLine(WeekDays.Saturday); // Saturday
Console.WriteLine(WeekDays.Sunday); // Sunday
Basics of Csharp language
24 Variable declaration

 int num;
 float rate;
 decimal amount;
 char code;
 bool isValid;
 string name;
 DateTime date;
Basics of Csharp language
25 Assignment

 num = 100;
 rate = 10.2f;
 amount = 100.50M;
 char code = 'C';
 bool isValid = true;
 string name = "Steve";
 DateTime date = new DateTime(2020, 12, 31);
Basics of Csharp language
26 Assignment: var keyword

 var num = 100;


 var date = new DateTime(2020, 12, 31);
Basics of Csharp language
27 Cast: number to number

 double price = (double)100;


 float price = (float)num;
 var num = (int) price;
 int day = (int)WeekDays.Friday;
 var wd = (WeekDays)5;
Basics of Csharp language
28 Cast: number to string

 string text = price.ToString();


 var amount = price.ToString("N2")
Basics of Csharp language
29 Cast: number to string

 string text = price.ToString();


 var amount = price.ToString("N2");
 string today = date.ToString("dd/MM/yyyy");
Basics of Csharp language
30 Cast: string to number

 int num = int.Parse("100");


 int num = int.Parse("100 0"); //Compiling error
 int num;
 bool canParse = int.TryParse("100 0", out num);
 bool canParse = int.TryParse("100,0", out num); //Compiling error if your
system is in English
 WeekDays wd = (WeekDays)Enum.Parse(typeof(WeekDays), "Wenesday");
 WeekDays wd;
 bool canParse = Enum.TryParse("Wenesday", out wd);
 bool.Parse(string);
 double.Parse(string);
 DateTime.Parse(string);
 Etc.
Basics of Csharp language
31
Input / Output

 int Console.Read();
 string Console.ReadLine();
 ConsoleKeyInfo Console.ReadKey();

 void Console.Write(string);
 void Console.Write(string, object[]);
 void Console.WriteLine(string);
 void Console.WriteLine(string, object[]);
Basics of Csharp language
32
Input / Output: Read example
static void Main(string[] args)
{
char ch;
do
{
int x = Console.Read();
ch = (char)x;
Console.WriteLine($"{ch} ASCII is {x}");
}
while (!char.IsWhiteSpace(ch));
}
Basics of Csharp language
33
Input / Output: ReadLine example

static void Main(string[] args)


{
Console.Write("How old are you ?: ");
string text = Console.ReadLine();
int age = int.Parse(text);
}
Basics of Csharp language
34
Input / Output: ReadKey example
static void Main(string[] args)
{
ConsoleKeyInfo key;
string code = string.Empty;
do
{
key = Console.ReadKey(true); //true means don’t display the pressed key

Console.Write("*");
code += key.KeyChar;
}
while (key.Key != ConsoleKey.Enter);
Console.WriteLine(code);
}
Basics of Csharp language
35 operators

 Same to C language
Basics of Csharp language
Conditional instructions
36

 Same to C language
Basics of Csharp language
Iterative instructions
37

 Same to C language
foreach(int a in myArray)
{
Console.WriteLine(a.ToString());
}
Basics of Csharp language
Arrays: one dimension
38

 int[] evenNums;
 string[] cities;
 int[] evenNums = new int[5] { 2, 4, 6, 8, 10 };
 string[] cities = new string[3] { "Mumbai", "London", "New York" };
 var evenNums = new int[] { 2, 4, 6, 8, 10 };
 var cities = new string[] { "Mumbai", "London", "New York" };
 int[] evenNums = { 2, 4, 6, 8, 10 };
 string[] cities = { "Mumbai", "London", "New York" };
Basics of Csharp language
Arrays: invalid arrays
39

 int[] evenNums = new int[]; //Syntax error


 int[] evenNums = new int[5] { 2, 4 }; //Syntax error
 var evenNums = { 2, 4, 6, 8, 10 }; //Syntax error
Basics of Csharp language
Arrays: Late initialization
40

 int[] evenNums;
 evenNums = new int[5];
 evenNums = new int[] { 2, 4, 6, 8, 10 };
Basics of Csharp language
Arrays: Access array elements
41

 int[] evenNums = new int[5];


 evenNums[0] = 2;
 evenNums[1] = 4;
 evenNums[6] = 12; //Throws run-time exception IndexOutOfRange
 Console.WriteLine(evenNums[0]);
 Console.WriteLine(evenNums[1]);
Basics of Csharp language
Arrays: Access array elements
42

int[] evenNums = { 2, 4, 6, 8, 10 };

for (int i = 0; i < evenNums.Length; i++)


Console.WriteLine(evenNums[i]);

for (int i = 0; i < evenNums.Length; i++)


evenNums[i] = evenNums[i] + 10;
Basics of Csharp language
Arrays: Access array with foreach
43

int[] evenNums = { 2, 4, 6, 8, 10 };
string[] cities = { "Mumbai", "London", "New York" };

foreach (var item in evenNums)


Console.WriteLine(item);

foreach (var city in cities)


Console.WriteLine(city);
Basics of Csharp language
44
Arrays: multidimensional arrays
 int[,] arr2d;
 int[,,] arr3d;
 int[,,,] arr4d;
 int[,,,,] arr5d;
Basics of Csharp language
45
Arrays: multidimensional arrays
int[,,] arr3d1 = new int[1, 2, 2]{ { {1, 2}, {3, 4} } };

int[,,,] arr4d2 = new int[1, 2, 2, 2]{ { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } } };
Basics of Csharp language
46
Arrays: access multidimensional arrays
arr2d[1, 0];
//return 3

arr3d2[1, 0, 1];
// returns 6

arr4d2[0, 1, 1, 0];
// returns 7
Basics of Csharp language
47
Arrays: Jagged Arrays or Array of Array
 int[][] jArray1 = new int[2][];
 int[][,] jArray2 = new int[3][,];

 int[][] jArray = new int[2][];


 jArray[0] = new int[3]{1, 2, 3};
 jArray[1] = new int[4]{4, 5, 6, 7 };
Basics of Csharp language
48
Arrays: Jagged Arrays or Array of Array
int[][] jArray = new int[2][]
{
new int[3]{1, 2, 3},
new int[4]{4, 5, 6, 7}
};

for(int i = 0; i < jArray.Length; i++)


{
for(int j = 0; j < (jArray[i]).Length; j++)
Console.WriteLine(jArray[i][j]);
}
Basics of Csharp language
Functions
49

 Same to C language
Basics of Csharp language
Functions: Optional parameters
50

 double Total(double price, int quantity = 1);


 double Total(double price, int quantity = 1, float tax = 0.1925);
 double Total(double price, int quantity = 1, float tax); //Syntax error

 var result = Total(100);


 var result = Total(100, 2);
 var result = Total(100, 3, 1.3);
Basics of Csharp language
Functions: Reference parameters
51

static void Main(string[] args)


{
float tax = 0.1925f;
Total(200, 14, ref tax);
Console.WriteLine($"Tax = {tax}");
}

static double Total(double price, int quantity, ref float tax)


{
if (quantity > 10)
tax *= 2;
var subTotal = price * quantity;
return subTotal + (subTotal * tax);
}
Basics of Csharp language
Functions: Function and array
52

public static void Main()


{
int[] nums = { 1, 2, 3, 4, 5 };
UpdateArray(nums);
foreach (var item in nums)
Console.WriteLine(item);
}
public static void UpdateArray(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
arr[i] = arr[i] + 10;
}
Array is a reference type
Basics of Csharp language
Functions: Output parameters
53

static void Main(string[] args)


{
float tax; //don't need to initialize output parameter
var total = Total(200, 14, out tax);
Console.WriteLine("Total = {0}, Tax = {1}", total, tax);
}
static double Total(double price, int quantity, out float tax)
{
tax = 0.1925f; //need to initialize output parameter inside the function
switch(quantity)
{
case int n when (n > 10):
tax *= 2;
break;
case int n when (n > 2):
tax *= 1;
break;
default:
tax = default;
break;
}
var subTotal = price * quantity;
return subTotal + (subTotal * tax);
}
Basics of Csharp language
Functions: params keyword parameters
54

static void Main(string[] args)


{
//float[] marks = { 8, 9, 8.5f };
//float total = TotalMarks(marks);
float total = TotalMarks(8, 9, 8.5f);
}

public static float TotalMarks(params float[] marks)


{
float total = 0;
foreach (var mark in marks)
total += mark;
return total;
}
Basics of Csharp language
Functions: Overloading
55

 Same name but different parameters in type or/and number

 double Total(double price);


 double Total(double price, int quantity);
 double Total(double price, int quantity, float tax);

 Total(100);
 Total(100, 2);
 Total(100, 2, 0.1925);
Basics of Csharp language
Functions: Delegate
56

 A delegate is a function type


 How pass function as an parameter to another function ?
 Why and when to use it ?
 JavaScript do it fine and very simply
 Java doesn’t support delegate
class Program
{
public static void PrintMarks(float[] marks)
57 {
foreach (var mark in marks)
Console.Write(mark + "\t");
}
public static void Printprice(float[] prices)
{
foreach (var mark in prices)
Console.WriteLine(mark.ToString("C0"));
}
public delegate void PrintFloatArray(float[] a);
public static void Print(PrintFloatArray function)
{
float[] data = { 1000, 200.50f, 14999.99f };
function(data);
}
static void Main(string[] args)
{
Print(Printprice);
}
}
Basics of Csharp language
Functions: Action<> & Func<> delegate
58

 There are generic delegate


 Action<> for void methods
 Func<> for returned methods
 Ex: Action<string, int> myFunction;
 Represent a void function with two parameters: first one is a string
and second one is an integer.
 Ex: Action MyFunction;
 Represent a void function without parameter.
 Ex: Func<string, int> MyFunction;
 Represent a function with one parameter string type, and return an
integer.
 Ex: Func<string> MyFunction
 Represent a function without parameter, and that return a string.
class Program
{
public static void PrintMarks(float[] marks)
59
{
foreach (var mark in marks)
Console.Write(mark + "\t");
}
public static void Printprice(float[] prices)
{
foreach (var mark in prices)
Console.WriteLine(mark.ToString("C0"));
}
public static void Print(Action<float[]> function)
{
float[] data = { 1000, 200.50f, 14999.99f };
function(data);
}
static void Main(string[] args)
{
Print(Printprice);
}
}
Basics of Csharp language
Functions: Anonymous function
60

 A function without a name


 The reuse occurrence of this function is one (1)
 We use delegate to create an anonymous function
class Program
{
public static void Printprice(float[] prices)
61 {
foreach (var mark in prices)
Console.WriteLine(mark.ToString("C0"));
}
public static void Print(Action<float[]> function)
{
float[] data = { 1000, 200.50f, 14999.99f };
function(data);
}
static void Main(string[] args)
{
Print
(
delegate (float[] marks)
{
foreach (var mark in marks)
Console.Write(mark + "\t");
}
);
}
}
Basics of Csharp language
Functions: Lambda expressions
62

 Symbol: =>
 syntax: (parameters) => expression; (parameters) => { //code }
 Use to write function with just one line
 Ex: int Increment(int a) => ++a;
 Ex: void Print(double price) => Console.WriteLine(price.ToString("C0"));
 Use to simplify anonymous functions
Print
(
(marks) =>
{
foreach (var mark in marks)
Console.Write(mark + "\t");
}
);
Basics of Csharp language
Functions: Predicate delegate
63

 Represents a method containing a set of criteria and checks whether the


passed parameter meets those criteria.
 A predicate delegate methods must take one input parameter and return boolean
static bool IsUpperCase(string str)
{
return str.Equals(str.ToUpper());
}
static void Main(string[] args)
{
Predicate<string> isUpper = IsUpperCase;
bool result = isUpper("hello world!!");
Console.WriteLine(result);
}
Basics of Csharp language
Functions: Predicate delegate
64

static void Main(string[] args)


{
Predicate<string> isUpper = delegate (string s)
{
return s.Equals(s.ToUpper());
};
}
static void Main(string[] args)
{
Predicate<string> isUpper = s => s.Equals(s.ToUpper());
bool result = isUpper("hello world!!");
}
FIN
65

Questions

You might also like