Lecture 3 - Introduction (Console Application)
Lecture 3 - Introduction (Console Application)
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,
name Valid
subject101 Valid
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.
}
} 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
}
} 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.
// Main Method
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
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
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";
class StringLiterals{
// Main Method
public static void Main(String[] args)
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{
Character Meaning
Value types
Reference types
Pointer types
• 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'
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;
using System;
namespace DataTypeApplication
using System;
namespace DataTypeApplication
{
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.
35
C# Type Conversion
• Type conversion is converting one type of data to another
type (known as Type Casting).
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.
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)
{