CS6004NI Application Development: Samyush - Maharjan@islingtoncollege - Edu.np
CS6004NI Application Development: Samyush - Maharjan@islingtoncollege - Edu.np
Application Development
samyush
Samyush.maharjan@islingtoncollege.edu.np
https://github.com/Samyush
2
C# Langauge
C# Language
● C# ● Java
1. using System; 1. import java.util.ArrayList;
2. namespace HelloWorld
2. class HelloWorld {
3. {
4. class Program 3. public static void main(String[] args) {
5. { 4. System.out.println("Hello, World!");
6. static void Main(string[] args)
5. }
7. {
8. Console.WriteLine("Hello World!"); 6. }
9. }
10. }
11. }
https://github.com/Samyush
| 4
C# Syntax
Comments, Statements, and Blocks
1. // Single-line comments look like this
2. /*
3. Multi-line comments look like this
4. */
6. namespace HelloWorld
7. { // an open brace indicates the start of a block
8. class Program
9. {
10. static void Main(string[] args)
11. {
12. Console.WriteLine("Hello World!"); // a statement
13. }
14. }
15. } // a close brace indicates the end of a block
| 5
C# Syntax
Top-level Statement (C# 9.0)
● Before C# 9.0 (.NET 5) Top-level Statement Rules:
1. using System;
A char is assigned using ' (single quotes) around a Unicode UTF-16 character as value.
1. char letter = 'A';
2. char digit = '1';
3. char symbol = '@';
● string
A string is assigned using " (double quotes) around zero or more characters value.
○ Literal string
1. string firstName = "John";
2. string lastName = "Doe";
3. string textWithDoubleQuote = "This \"text\" has double quotes";
4. string phoneNumber = "+977 980-0000000";
5. string filePath = "C:\\projects\\HelloWorld\\README.md";
6. string multiLineText = "The quick brown fox \njumps over the lazy dog";
| 10
C# Variables
Storing Text
○ Verbatim string
1. string filePath = @"C:\projects\HelloWorld\README.md";
2. string multiLineText = @"The quick brown fox
3. jumps over the lazy dog";
4. // without @ prefix, throws an error
○ Interpolated string
5. string firstName = "John";
6. string lastName = "Doe";
7. string fullName = $"{firstName} {lastName}";
| 11
C# Variables
Storing Numbers
● Integral numeric types
1. // integer means negative or positive whole number, -2,147,483,648 to 2,147,483,647
2. int positiveNumber = 12;
3. int negativeNumber = -12;
6. // Digit Separators
7. int billion = 1_000_000_000;
8. // other integer types: sbyte, byte, short, ushort, long, ulong, nint, nuint
Other integer types: sbyte, byte, short, ushort, long, ulong, etc.
| 12
C# Variables
Storing Numbers
● Implicit Casting (automatically)
● Floating-point numeric types char -> int -> long -> float -> double
1. // f or F suffix is required for a float literal int myInt = 9;
2. float realNumber = 2.3f;
// Automatic casting: int to double
3. // float are accurate up to 7 decimal places
4. float floatPrecision = 1F/3; // => 0.33333334 double myDouble = myInt;
Note: C# implicitly types non-floating-point numbers as an int and floating-point numbers as a double.
| 13
C# Variables
Storing Numbers
● Floating-point numeric types
1. // f or F suffix is required for a float literal
2. float realNumber = 2.3f;
3. // float are accurate up to 7 decimal places
4. float floatPrecision = 1F/3; // => 0.33333334
Note: C# implicitly types non-floating-point numbers as an int and floating-point numbers as a double.
| 14
C# Variables
Storing Boolean
Booleans can only contain one of the two literal values true or false.
1. bool weekday = true;
2. bool weekend = false;
| 15
C# Variables
Inferring the Types
● var
1. var myMoney = 1_000.5m; // decimal
2. var lastName = "Doe"; // string
3. var letter = 'A'; // char
4. var weekday = true; // bool
● object
1. object height = 1.83; // storing a double in an object
2. object name = "John"; // storing a string in an object
3. string nameStr = (string)name; // we must explicitly cast to tell compiler it is a string
● dynamic
1. dynamic age = 25; // storing a int in an object
2. dynamic letter = 'A'; // storing a char in an object
3. char letterChar = letter; // no need to explicitly cast to tell compiler it is a char
Note: The flexibility comes at the cost of performance, avoid using object or dynamic .
| 16
C# Variables
Default Values for Types
1. // all number types have default value of 0
2. int naturalNumber = default; // => 0
3. decimal realNumber = default; // => 0
| 17
C# Variables
What type would you choose for the following "numbers"?
● A book's title
● A person's height
● A person's telephone number
● A person's age
● A person's salary
| 18
C# Variables
What type would you choose for the following "numbers"?
● A book's title → string
● A person's height → float, double
● A person's telephone number → string
● A person's age → int
● A person's salary → decimal
| 19
Continuation on Week 2 ….
| 20
C# Arrays
C# Arrays
Creating Arrays
1. string[] names; // can reference any size array of strings
2. names = new string[2]; // allocating memory for four strings in an array
3. names[0] = "John"; // storing John at 0 positions
4. names[1] = "Jack"; // storing Tom at 1 positions
6. // implicitly-typed array
7. var a = new[] { 1, 10, 100, 1_000, 10_000 }; // int[]
8. var b = new[] { "hello", null, "world" }; // string[]
| 22
C# Arrays
Jagged Arrays (Array of Arrays)
1. var jaggedArray = new int[][]
2. {
3. new int[] { 1, 3, 5, 7, 9 },
4. new int[] { 0, 2, 4, 6 },
5. new int[] { 11, 22 }
6. };
7. Console.WriteLine(jaggedArray[1][3]); // 6
| 23
C# Arrays
Multidimensional Arrays
1. // Two-dimensional array.
2. var array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
3. Console.WriteLine(array2D[2,1]); // => 6
| 24
C# Arrays
Useful Array Methods
1. var a = new[] { 1, 10, 100, 1_000, 10_000 }; // int[]
2. var b = new[] { "hello", null, "world" }; // string[]
3. // length of an array
4. Console.WriteLine(a.Length); // => 5
5. // check if array contains a value
6. Console.WriteLine(b.Contains("hello")); // => true
7. Array.Reverse(b);
8. Console.WriteLine(b[0]); // => world
| 25
C# Console App
C# Console App
Creating and Running a Console App
● Using Visual Studio IDE
● Using CLI
○ Creating a Console App
■ dotnet new console -n HelloWorld --framework net6.0
○ Running the App
■ cd HelloWorld
■ dotnet run
| 27
C# Console App
Console Output
● Content of Project.cs
1. // See https://aka.ms/new-console-template for more information
2. Console.WriteLine("Hello, World!");
| 28
C# Console App
Console User Input
● Getting text input from the user
1. // Getting text input from the user
2. Console.Write("Type your first name and press ENTER: ");
3. string? firstName = Console.ReadLine();
4. Console.Write("Type your age and press ENTER: ");
5. string? age = Console.ReadLine();
3. Console.WriteLine();
4. Console.WriteLine($"Key: {key.Key}, Char: {key.KeyChar}, Modifiers: {key.Modifiers}");
| 29
C# Console App
Passing arguments to a console app
● Passing arguments to a console app
○ Visual Studio
■ Navigate to Project > Arguments Properties, select the Debug tab
■ In the Application arguments box, enter some arguments: