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

Lecture 3 - Introduction (Console Application)

This document discusses visual programming and the content of a course on visual programming using C#. The course covers C# syntax and constructs like methods, exceptions, structs, enums, classes, collections, interfaces, inheritance, generics, LINQ, XAML, tasks and asynchronous programming. It provides examples of variable naming rules, data types, literals, and integer and floating-point literals in C#.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lecture 3 - Introduction (Console Application)

This document discusses visual programming and the content of a course on visual programming using C#. The course covers C# syntax and constructs like methods, exceptions, structs, enums, classes, collections, interfaces, inheritance, generics, LINQ, XAML, tasks and asynchronous programming. It provides examples of variable naming rules, data types, literals, and integer and floating-point literals in C#.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Visual Programming

Department of CSE, QUEST


Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content
Review of C# Syntax: Overview of Writing Applications using C#, Data types,
Operators, and Expressions
C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods

Developing the Code for a Graphical Application: Implementing Structs and Enums
Implementing Type-safe Collections: Creating Classes, Organizing Data into Collections,

Department of CSE, QUEST


Dr. Irfana Memon
Handling Events, Defining and Implementing Interfaces
Creating a Class Hierarchy by Using Inheritance, Extending .NET Framework Classes,
Creating Generic Types
Accessing a Database: Creating and Using Entity Data Models, Querying and Updating Data
by Using LINQ
Designing the User Interface for a Graphical Application: Using XAML, Binding Controls to
Data, Styling a User Interface
Improving Application Performance and Responsiveness: Implementing Multitasking by
using Tasks and Lambda Expressions
2
Performing Operations Asynchronously, Synchronizing Concurrent Access to Data
Review of C# Syntax:
Overview of Writing
Applications using C#,

Department of CSE, QUEST


Dr. Irfana Memon
Data types, Operators,
and Expressions
3
How to declare variable in C#
• An example to declare a variable in C#.
 int age;
 int age = 24;
• Implicitly typed variables
 Alternatively in C#, we can declare a variable without

Department of CSE, QUEST


Dr. Irfana Memon
knowing its type using var keyword. Such variables are
called implicitly typed local variables.
 Variables declared using var keyword must be initialized
at the time of declaration.
 var value = 5;
 The compiler determines the type of variable from the
value that is assigned to the variable.
4
 In the above example, value is of type int. This is
equivalent to: int value; value = 5;
Rules for naming variable in C#
• There are certain rules we need to follow while naming a
variable.
• The rules for naming a variable in C# are:
 The variable name can contain letters (uppercase and
lowercase), underscore( _ ) and digits only.

Department of CSE, QUEST


Dr. Irfana Memon
 The variable name must start with either letter,
underscore or @ symbol.
 C# is case sensitive. It means age and Age refers to 2
different variables.
 A variable name must not be a C# keyword.
 For example, if, for, using can not be a variable name.
5
Rules for naming variable in C#
Rules for naming variables in C#

Variable Names Remarks

name Valid

subject101 Valid

Department of CSE, QUEST


Dr. Irfana Memon
Valid (Best practice for naming private
_age
member variables)

@break Valid (Used if name is a reserved keyword)

101subject Invalid (Starts with digit)

your_name Valid
6
your name Invalid (Contains whitespace)
C# Literals/Constant
• Let's look at the following statement:
• int number = 41;
Here,int is a data type
• number is a variable and 41 is a literal
• Literals are fixed values that appear in the program.

Department of CSE, QUEST


Dr. Irfana Memon
• They do not require any computation.

Literals can be of the following types:


Integer Literals
Floating-point Literals
Character Literals
String Literals
Boolean Literals 7
Integer Literals
• Integer literals are used to initialize variables of integer data
types i.e. sbyte, short, int, long, byte, ushort, uint and ulong.
• If an integer literal ends with L or l, it is of type long.+
• For best practice use L (not l).
 long value1 = 4200910L;

Department of CSE, QUEST


Dr. Irfana Memon
 long value2 = -10928190L;
• If an integer literal starts with a 0x, it represents hexadecimal
value.
• Number with no prefixes are treated as decimal value.
• Octal and binary representation are not allowed in C#.
 int decimalValue = 25;
 int hexValue = 0x11c;// decimal value 284
8
Example Program: Integer Literals
// C# program to illustrate the use of Integer Literals
using System;
class IntegerLiterals{
// Main method
public static void Main(String[] args)

Department of CSE, QUEST


Dr. Irfana Memon
{
// decimal-form literal
int a = 101;
// Hexa-decimal form literal
int b = 0xFace;
Console.WriteLine(a);
Console.WriteLine(b);

}
} 9
Example Program: Integer Literals
// C# program to illustrate the use of Integer Literals
using System;
class IntegerLiterals{
// Main method
101
public static void Main(String[] args) 64206

Department of CSE, QUEST


Dr. Irfana Memon
{
// decimal-form literal
int a = 101;
// Hexa-decimal form literal
int b = 0xFace;
Console.WriteLine(a);
Console.WriteLine(b);

}
} 10
Floating point Literals
• Floating point literals are used to initialize variables of float
and double data types.
• If a floating point literal ends with a suffix f or F, it is of type
float.
• Similarly, if it ends with d or D, it is of type double.

Department of CSE, QUEST


Dr. Irfana Memon
• If neither of the suffix is present, it is of type double
by default.
• These literals contains e or E when expressed in scientific
notation.
 double number = 24.67;// double by default
 float value = -12.29F;
 double scientificNotation = 6.21e2;// equivalent to 6.21 x
102 i.e. 621 11
Example: Floating point Literals

• Double d = 3.14145 // Valid


• Double d = 312569E-5 // Valid
• Double d = 125E // invalid: Incomplete exponent
• Double d = 784f // valid
• Double d = .e45 // invalid: missing integer or fraction

Department of CSE, QUEST


Dr. Irfana Memon
12
Example Program: Floating point
Literals
// C# program to illustrate the use of floating-point literals
using System;
class FloatingPoint {

// Main Method

Department of CSE, QUEST


Dr. Irfana Memon
public static void Main(String[] args)
{
// decimal-form literal
double a = 101.230;

double b = 0123.222;

Console.WriteLine(a);
Console.WriteLine(b); 13
}
}
Example Program: Floating point
Literals
// C# program to illustrate the use of floating-point literals
using System;
class FloatingPoint {
101.23
// Main Method 123.222

Department of CSE, QUEST


Dr. Irfana Memon
public static void Main(String[] args)
{
// decimal-form literal
double a = 101.230;

double b = 0123.222;

Console.WriteLine(a);
Console.WriteLine(b); 14
}
}
Example Program: Floating point
Literals
// C# program to illustrate the use of floating-point literals
using System;
class FloatingPoint {
101.23
// Main Method 123.222

Department of CSE, QUEST


Dr. Irfana Memon
public static void Main(String[] args)
{
// decimal-form literal Note: By default, every floating-point
double a = 101.230; literal is of double type and hence we
can’t assign directly to float variable. But
double b = 0123.222; we can specify floating-point literal as
float type by suffixed with f or F. We can
Console.WriteLine(a); specify explicitly floating-point literal as
Console.WriteLine(b); the double type by suffixed with d or D, 15
} of course, this convention is not
} required.
Character Literals
• Character literals are used to initialize variables of char data
types.
• Character literals are enclosed in single quotes.
• For example, 'x', 'p', etc.
• They can be represented as character, hexadecimal escape

Department of CSE, QUEST


Dr. Irfana Memon
sequence, unicode representation or integral values casted to
char.
 char ch1 = 'R';// character
 char ch2 = '\x0072';// hexadecimal
 char ch3 = '\u0059';// unicode
 char ch4 = (char)107;// casted from integer

16
String Literals
• Literals which are enclosed in double quotes(“”) or starts
with @”” are known as the String literals.
• For example, "Hello", "Easy Programming", @"Hello!" etc.
 string firstName = "Richard";
 string lastName = " Feynman";

Department of CSE, QUEST


Dr. Irfana Memon
17
Example program: String Literals
// C# program to illustrate the use of String literals
using System;

class StringLiterals{

// Main Method
public static void Main(String[] args)

Department of CSE, QUEST


Dr. Irfana Memon
{

String s = "Hello ";


String s2 = @"HelloHello";

Console.WriteLine(s);
Console.WriteLine(s2);
} 18
}
Boolean Literals
• true and false are the available boolean literals.
• They are used to initialize boolean variables.
For example:
bool isValid = true;
bool isPresent = false;
// C# program to illustrate the use of boolean literals
using System;
class BooleanLiteral{

Department of CSE, QUEST


Dr. Irfana Memon
// Main Method
public static void Main(String[] args)
{
bool b = true;
bool c = false;
// these will give compile time error
// bool d = 0;
// bool e = 1;
// Console.WriteLine(d);
// Console.WriteLine(e);
Console.WriteLine(b); 19
Console.WriteLine(c);
}
}
Escape sequence characters
• C# also supports escape sequence characters such as:

Character Meaning

\' Single quote


\" Double quote

Department of CSE, QUEST


Dr. Irfana Memon
\\ Backslash
\n Newline
\r Carriage return
\t Horizontal Tab
\a Alert
\b Backspace 20
Data Types in C#
• The variables in C#, are categorized into the following types:

 Value types
 Reference types
 Pointer types

Department of CSE, QUEST


Dr. Irfana Memon
21
Data Types in C# (Value types)
• Value type variables can be assigned a value directly.
• They are derived from the class System.DataType.
• The value types directly contain data.
• Some examples are int, char, and float, which stores
numbers, alphabets, and floating point numbers,

Department of CSE, QUEST


Dr. Irfana Memon
respectively.
• When you declare an int type, the system allocates memory
to store the value.

• using System;
• namespace DataType
22
Data Types in C# (Value types)
• The following table lists the available value types in C# :
Type Represents Range Default Value
bool Boolean value True or False False
byte 8-bit unsigned 0 to 255 0
integer
char 16-bit Unicode U +0000 to U +ffff '\0'

Department of CSE, QUEST


Dr. Irfana Memon
character
decimal 128-bit precise 0.0M
28
decimal values with (-7.9 x 10 to 7.9 x
28-29 significant 1028) / 100 to 28
digits
-324
double 64-bit double- (+/-)5.0 x 10 to 0.0D
308
precision floating (+/-)1.7 x 10
point type
23
float 32-bit single- 0.0F
-3.4 x 1038 to + 3.4 x
precision floating
1038
point type
Data Types in C# (Value types)
• The following table lists the available value types in C# :

Type Represents Range Default Value

int 32-bit signed integer -2,147,483,648 to 0


type 2,147,483,647

Department of CSE, QUEST


Dr. Irfana Memon
long 64-bit signed integer - 0L
type 9,223,372,036,854,7
75,808 to
9,223,372,036,854,7
75,807
sbyte 8-bit signed integer -128 to 127 0
type
short 16-bit signed integer 0 24
-32,768 to 32,767
type
Data Types in C# (Value types)
• The following table lists the available value types in C# :

Type Represents Range Default Value

uint 32-bit unsigned 0


0 to 4,294,967,295
integer type

Department of CSE, QUEST


Dr. Irfana Memon
ulong 64-bit unsigned 0 to 0
integer type 18,446,744,073,709,
551,615
ushort 16-bit unsigned 0
0 to 65,535
integer type

25
Boolean (bool) Data Type in C#
(Value types)
• Has two possible values : True and False
• Default value is False
• Boolean variables are generally used in check conditions
such as in if statement
using System;

Department of CSE, QUEST


Dr. Irfana Memon
namespace DataType
{
class BooleanExample
{
public static void Main(string[] args)
{
bool isValid = true;
Console.WriteLine(isValid);
26
}
}
}
Boolean (bool) Data Type in C#
(Value types)
• Has two possible values : True and False
• Default value is False
• Boolean variables are generally used in check conditions
such as in if statement
using System;

Department of CSE, QUEST


Dr. Irfana Memon
namespace DataType
{
class BooleanExample
{
public static void Main(string[] args)
{
bool isValid = true;
Console.WriteLine(isValid);
27
} the output will be:
} True
}
Data Types in C# (Value types)
• To get the exact size of a type or a variable on a particular
platform, you can use the sizeof method.
• The expression sizeof(type) yields the storage size of the
object or type in bytes.

Department of CSE, QUEST


Dr. Irfana Memon
28
Data Types in C# (Value types)
• Following is an example to get the size of int type on any
machine :

using System;
namespace DataTypeApplication

Department of CSE, QUEST


Dr. Irfana Memon
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.WriteLine("Size of int: “+ sizeof(int));
Console.ReadLine();
} 29
}
}
Data Types in C# (Value types)
• Following is an example to get the size of int type on any
machine :

using System;
namespace DataTypeApplication
{

Department of CSE, QUEST


Dr. Irfana Memon
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.ReadLine();
}
}
}
30
When the above code is compiled and executed, it produces the
following result −
Size of int: 4
Data Types in C# (Reference types)
• The reference types do not contain the actual data stored in a
variable, but they contain a reference to the variables.
• In other words, they refer to a memory location.
• Using multiple variables, the reference types can refer to a
memory location.

Department of CSE, QUEST


Dr. Irfana Memon
• If the data in the memory location is changed by one of the
variables, the other variable automatically reflects this
change in value.
• Example of built-in reference types
are: object, dynamic, and string.

31
Data Types in C# (Reference types)
• Built-in reference types are: object, dynamic, and string.

Object Type
• The Object Type is the ultimate base class for all data types
in C# Common Type System (CTS).
• Object is an alias for System.Object class.

Department of CSE, QUEST


Dr. Irfana Memon
• The object types can be assigned values of any other types,
value types, reference types, predefined or user-defined
types.
• However, before assigning values, it needs type conversion.
• When a value type is converted to object type, it is
called boxing and on the other hand, when an object type is
converted to a value type, it is called unboxing. 32
Data Types in C# (Reference types)
• Built-in reference types are: object, dynamic, and string.
Dynamic Type
• You can store any type of value in the dynamic data type
variable.
• Type checking for these types of variables takes place at run-

Department of CSE, QUEST


Dr. Irfana Memon
time.
Syntax for declaring a dynamic type is:
dynamic <variable_name> = value;
For example,
dynamic d = 20;
Dynamic types are similar to object types except that type
checking for object type variables takes place at compile time,
33
whereas that for the dynamic type variables takes place at run
time.
Data Types in C# (Reference types)
• Built-in reference types are: object, dynamic, and string.
String Type
• The String Type allows you to assign any string values to a
variable.
• The string type is an alias for the System.String class.

Department of CSE, QUEST


Dr. Irfana Memon
• It is derived from object type.
• The value for a string type can be assigned using string
literals in two forms: quoted and @quoted.
For example,
 String str = "Tutorials Point";
 A @quoted string literal looks as follows −
 @"Tutorials Point";
34
The user-defined reference types are: class, interface, or
delegate.
Data Types in C# (Pointer types)
• Pointer type variables store the memory address of another
type.
• Pointers in C# have the same capabilities as the pointers in C
or C++.
Syntax for declaring a pointer type is:
type* identifier;

Department of CSE, QUEST


Dr. Irfana Memon
For example,
char* cptr;
int* iptr;

35
C# Type Conversion
• Type conversion is converting one type of data to another
type (known as Type Casting).

• In C#, type casting has two forms:

Implicit type conversion − These conversions are performed

Department of CSE, QUEST


Dr. Irfana Memon
by C# in a type-safe manner. For example, are conversions from
smaller to larger integral types and conversions from derived
classes to base classes.

Explicit type conversion− These conversions are done


explicitly by users using the pre-defined functions. Explicit
conversions require a cast operator.
36
The following example shows an explicit type conversion −
C# Type Conversion
The following example shows an explicit type conversion −
using System;
namespace TypeConversionApplication
{
class ExplicitConversion
This code produces the following result − {

Department of CSE, QUEST


Dr. Irfana Memon
5673 static void Main(string[] args)
{
double d = 5673.74;
int i; // cast double to int.
i = (int)d;
Console.WriteLine(i);
Console.ReadKey();
}
} 37
}
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
S. No. Methods & Description
1 ToBoolean
Converts a type to a Boolean value, where possible.
2 ToByte
Converts a type to a byte.

Department of CSE, QUEST


Dr. Irfana Memon
3 ToChar
Converts a type to a single Unicode character, where possible.
4 ToDateTime
Converts a type (integer or string type) to date-time structures.
5 ToDecimal
Converts a floating point or integer type to a decimal type.
6 ToDouble
Converts a type to a double type. 38
7 ToInt16
Converts a type to a 16-bit integer.
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
S. No. Methods & Description
8 ToInt32
Converts a type to a 32-bit integer.
9 ToInt64
Converts a type to a 64-bit integer.

Department of CSE, QUEST


Dr. Irfana Memon
10 ToSbyte
Converts a type to a signed byte type.
11 ToSingle
Converts a type to a small floating point number.
12 ToString
Converts a type to a string.

39
C# Type Conversion Methods
C# provides the following built-in type conversion methods:
S. No. Methods & Description
13 ToType
Converts a type to a specified type.
14 ToUInt16
Converts a type to an unsigned int type.

Department of CSE, QUEST


Dr. Irfana Memon
15 ToUInt32
Converts a type to an unsigned long type.
16 ToUInt64
Converts a type to an unsigned big integer.

40
Exercise: C# Type Conversion
using System; Methods
namespace TechStudyCSharp
{
class Program
C# Program to find the Size of data types
{
static void Main(string[] args)
{

Department of CSE, QUEST


Dr. Irfana Memon
Console.WriteLine("Size of char: " + sizeof(char));
Console.WriteLine("Size of Short: " + sizeof(short));
Console.WriteLine("Size of int: " + sizeof(int));
Console.WriteLine("Size of long: " + sizeof(long));
Console.WriteLine("Size of float: " + sizeof(float));
Console.WriteLine("Size of double: " + sizeof(double));
Console.ReadKey();
}
41
}
}
Exercise: C# Type Conversion
using System; Methods
namespace TechStudyCSharp
{
class Program
C# Program to find the Size of data types
{
static void Main(string[] args)
{

Department of CSE, QUEST


Dr. Irfana Memon
Console.WriteLine("Size of char: " + sizeof(char));
Console.WriteLine("Size of Short: " + sizeof(short));
Console.WriteLine("Size of int: " + sizeof(int));
Console.WriteLine("Size of long: " + sizeof(long));
Console.WriteLine("Size of float: " + sizeof(float));
Console.WriteLine("Size of double: " + sizeof(double));
Console.ReadKey();
}
42
}
}
Exercise: C# Type Conversion
using System; Methods
namespace TechStudyCSharp
{
C# Program
class Programto Print ASCII Value
{
static void Main(string[] args)
{
char c;

Department of CSE, QUEST


Dr. Irfana Memon
Console.WriteLine("Enter a character: ");
c = Convert.ToChar(Console.ReadLine());
Console.WriteLine("\nASCII Value of " +c+" "+ Convert.ToInt32(c));
Console.ReadKey();
}
}
}
43
Exercise: C# Type Conversion
using System; Methods
namespace TechStudyCSharp
{
C# Program
class Programto Print ASCII Value
{
static void Main(string[] args)
{

Department of CSE, QUEST


Dr. Irfana Memon
char c;
Console.WriteLine("Enter a character: ");
c = Convert.ToChar(Console.ReadLine());
Console.WriteLine("\nASCII Value of " +c+" "+ Convert.ToInt32(c));
Console.ReadKey();
}
}
} 44
Wish You Good Luck

Dr. Irfana Memon


45

Department of CSE, QUEST

You might also like