0% found this document useful (0 votes)
30 views32 pages

CS6004NI Application Development: Samyush - Maharjan@islingtoncollege - Edu.np

Uploaded by

munagiri48
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)
30 views32 pages

CS6004NI Application Development: Samyush - Maharjan@islingtoncollege - Edu.np

Uploaded by

munagiri48
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/ 32

CS6004NI

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. */

5. using System; // a semicolon indicates the end of a statement

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;

2. namespace HelloWorld ● Only one top-level statement in one


3. {
4. class Program compilation unit.
5. {
6. static void Main(string[] args) ● The program cannot have a declared entry
7. {
8. Console.WriteLine("Hello World!"); point (Main method).
9. }
10. } ● The top-level statements cannot be
11. }
enclosed in a namespace.
● After C# 9.0 (.NET 5)
1. using System;
● Additional types can be declared only after

2. Console.WriteLine("Hello World!"); all top-level statements.


| 6
Naming Identifiers
An identifier can: An identifier cannot:
● start with an _ (underscore) ● start with a digit
● contain underscores ● start with a symbol, unless it is a keyword.
● contain digits (0123456789) E.g. @public
● contain upper and lower case letters (case ● contain more than 511 characters
sensitive) ● contain @ sign after its first character
● start with an @ sign
○ used when using a keyword as an
identifier. E.g. @public
○ otherwise @name is same as name
| 7
Naming Conventions
Naming Convention Examples Used for
Local variables,
email, userName, dateOfBirth,
Camel case private/internal fields
_userService
(prefix them with _)
Types (class, interface,
Email, UserName, DateOfBirth, record etc.), non-private
Title / Pascal case fields, and other members
IUserService
like methods

Read more on Naming Conventions


| 8
C# Variables
C# Variables
Storing Text
● char

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;

4. // unsigned integer means positive whole number, 0 to 4,294,967,295


5. uint naturalNumber = 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;

5. // d or D suffix is optional for double literal ● Explicit Casting (manually)


6. double anotherRealNumber = 2.3; double -> float -> long -> int -> char
7. // double are accurate up to 16 decimal places double myDouble = 9.78;
8. double doublePrecision = 1D/3; // => 0.3333333333333333
// Manual casting: double to int
9. // m or M suffix is required for a double literal int myInt = (int) myDouble;
10. decimal myMoney = 1_000.5m;
11. // decimal are accurate up to 29 decimal places
12. decimal decimalPrecision = 1M/3; // => 0.3333333333333333333333333333

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

5. // d or D suffix is optional for double literal


6. double anotherRealNumber = 2.3;
7. // double are accurate up to 16 decimal places
8. double doublePrecision = 1D/3; // => 0.3333333333333333

9. // m or M suffix is required for a double literal


10. decimal myMoney = 1_000.5m;
11. // decimal are accurate up to 29 decimal places
12. decimal decimalPrecision = 1M/3; // => 0.3333333333333333333333333333

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

4. bool weekday = default; // => false

5. // char type have default value of '\u0000', which is decimal equivalent is 0.


6. char weekday = default; // => '\u0000'

7. // Any reference type have default value of null


8. string name = default; // => null

9. DateTime dateTime = default; // => 01/01/0001 00:00:00

| 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

5. string[] names2 = { "John", "Jack" };

6. // implicitly-typed array
7. var a = new[] { 1, 10, 100, 1_000, 10_000 }; // int[]
8. var b = new[] { "hello", null, "world" }; // string[]

9. // accessing array elements by index position


10. Console.WriteLine(names[2]); // => Tom
11. Console.WriteLine(a[4]); // => 10000
12. Console.WriteLine(b[^3]); // => hello

| 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

4. // Three-dimensional array with dimensions specified.


5. var array3D = new int[2, 2, 3] {
6. { { 1, 2, 3 }, { 4, 5, 6 } },
7. { { 7, 8, 9 }, { 10, 11, 12 } }
8. };
9. Console.WriteLine(array3D[1,0,2]); // => 9

| 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

Useful Array methods using System.Linq namespace:


9. using System.Linq;

10. var numbers = new[] { 10, 100, 10_000, 1_000, 1 }; // int[]


11. Console.WriteLine(a.Min()); // => 1
12. Console.WriteLine(a.Max()); // => 10000
13. Console.WriteLine(a.Sum()); // => 11111

| 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!");

● Formatting the Output


○ Using numbered positional arguments
1. Console.WriteLine(format: "Hello {0}, Happy {1}!", arg0: name, arg1: occasion);
2. // or
3. Console.WriteLine("Hello {0}, Happy {1}!", name, occasion);

○ Using interpolated strings


4. Console.WriteLine($"Hello {name}, Happy {occasion}!");

| 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();

6. Console.WriteLine( $"Hello {firstName}, you look good for {age}.");

● Getting key input from the user (Console.ReadKey)


1. Console.Write("Press any key combination: ");
2. ConsoleKeyInfo key = Console.ReadKey();

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:

firstarg second-arg third:arg "fourth arg"


■ Save the changes
■ Run the console app
○ CLI
■ Navigate to project folder in a Terminal
■ Run the console app with arguments:

dotnet run firstarg second-arg third:arg "fourth arg"


| 30
C# Console App
Accessing arguments passed to a console app
● In the Project.cs
1. …
2. static void Main(string[] args)
3. {
4. Console.WriteLine($"There are {args.Length} arguments.");
5. Console.WriteLine($"First arg: {args[0]} and last arg: {args[^1]}.");
6. }
7. …

or using Top-level Statement (C# 9.0)


8. using System;

9. Console.WriteLine($"There are {args.Length} arguments.");


10. Console.WriteLine($"First arg: {args[0]} and last arg: {args[^1]}.");

Note: if no argument is passed, dotnet will throw an error


| 31
APPLICATION DEVELOPMENT

December 29, 2024 CU****** APPLIED MACHINE LEARNING 32

You might also like