Visual C# .Net - Windows Form Application
Visual C# .Net - Windows Form Application
Visual C# .Net - Windows Form Application
NET:
Console
Applications
and Windows
Forms
Visual Studio .NET is a Microsoft-integrated
development environment (IDE) that can be used
for developing consoles, graphical user interfaces
(GUIs) and Windows Forms. This book explores the
use of C# for creating a variety of applications that
run on the .NET framework. The C# syntax is
explored through the use of several examples that
allow the user to create applications in console
mode, interact with objects in windows forms,
establish connection to a relational database and
explore the use of XML documents. Eight scenarios
for the adoption of this technology are also
exploited.
July, 2018
Visual C#.NET: Console Applications and 2018
Windows Forms
Table of Contents
Acronyms....................................................................................................................................... 4
Glossary ......................................................................................................................................... 5
1. Introduction............................................................................................................................... 6
1.1 Contextualization ................................................................................................................ 6
1.2 Objectives ............................................................................................................................ 6
1.3 Book Structure..................................................................................................................... 6
4. C# syntax ................................................................................................................................. 15
4.1 Variables ............................................................................................................................ 15
4.2 Operators .......................................................................................................................... 17
4.3 if-else statement ............................................................................................................... 19
4.4 Switch statement .............................................................................................................. 20
4.5 For loop ............................................................................................................................. 20
4.6 While loop ......................................................................................................................... 21
4.7 Arrays ................................................................................................................................ 22
4.8 Arrays class ........................................................................................................................ 25
4.9 Functions ........................................................................................................................... 26
4.10 Class................................................................................................................................. 28
4.11 Structs ............................................................................................................................. 31
4.12 Strings .............................................................................................................................. 33
4.13 Exceptions ....................................................................................................................... 35
4.14 Files.................................................................................................................................. 38
4.15 Collections ....................................................................................................................... 41
4.16 Databases ........................................................................................................................ 42
4.17 XML.................................................................................................................................. 44
5. Scenarios ................................................................................................................................. 48
5.1 Console Application: Price of products ............................................................................. 48
5.2 Console Application: Prime numbers ................................................................................ 48
Page 2
Visual C#.NET: Console Applications and 2018
Windows Forms
5.3 Console Application: Sum of digits .................................................................................... 49
5.4 Console Application: Sum elements of an array ............................................................... 50
5.5 Windows Forms: Dealing with boxes ................................................................................ 50
5.6 Windows Forms: Working with Strings ............................................................................. 52
5.7 Windows Forms: Interacting with databases .................................................................... 54
5.8 Windows Forms: Interacting with XML files ..................................................................... 58
Bibliography ................................................................................................................................ 62
Page 3
Visual C#.NET: Console Applications and 2018
Windows Forms
Acronyms
C# – C Sharp
Page 4
Visual C#.NET: Console Applications and 2018
Windows Forms
Glossary
Array – a data structure that contains a group of elements. Typically these elements are all of
the same data type, such as an integer or string. Arrays are commonly used in computer
programs to organize data so that a related set of values can be easily sorted or searched.
Console – the combination of display monitor and keyboard (or other device that allows
input). Another term for console is terminal.
LINQ – component of Microsoft .NET that adds query functionality in some .NET programming
languages.
List – abstract data structure that implements an ordered collection of values, where the same
value can occur more than once.
Prime number – natural numbers that have only two different divisors: the 1 and itself.
Relational database – a collective set of multiple data sets organized into tables, records and
columns. RDBs establish a well-defined relationship between database tables. Tables
communicate and share information, which facilitates data searchability, organization and
reporting.
SQL Injection – SQL injection attacks are a type of injection attack, in which SQL commands are
injected into data-plane input in order to effect the execution of predefined SQL commands.
SQL Server – a SQL-based relational database management system designed for use in
corporate applications, both on premises and in the cloud.
Page 5
Visual C#.NET: Console Applications and 2018
Windows Forms
1. Introduction
1.1 Contextualization
It is agreed that different programming languages are suitable for the development of
different applications. In fact, today, there is a range of languages suitable for the
development of each type of application, be it a scientific application, a web application, an
application for data management, etc. There is no single programming language suitable for
any application development. On the contrary, it is common for the development of the same
application to make use of different languages.
The evolution of programming languages leads to the emergence of new, more efficient
languages, giving programming new horizons in the face of constant demands on the part of
companies. Visual C#.NET is the programming language created by Microsoft and specially
designed for the development of applications on the .NET platform. This language and its
associated platform promise to radically change the way applications are developed for
Windows, and also for the Internet.
1.2 Objectives
Having as a philosophy the learning by doing, this book proposes a step-by-step learning
approach in which the concepts are accompanied by practical examples. It intends to give to
the reader a general perception and knowledge of each component of Visual C#.NET and to
solve practical exercises to consolidate the presented concepts. Finally, it presents a set of mini
projects developed in Visual C#.NET that can help the reader to consolidate the addressed
contents.
Introduction – a brief contextualization of the book, objectives and its structure is given;
The basic of console applications – provides a global overview about the process of
creating console applications using Visual Studio;
The basic of windows forms – provides a global overview about the process of creating
windows forms applications using Visual Studio;
C# syntax – gives a detailed overview about the syntax of C#. Several examples are
provided associated to each component and C# instruction;
Scenarios – numerous scenarios of creating mini projects in Visual C# are given. These
scenarios target console applications and windows forms.
Page 6
Visual C#.NET: Console Applications and 2018
Windows Forms
3. The name of the project must be specified in the name box. It is also important to select
the folder in which the project will be saved;
4. The new created project appears in Solution Explorer. Associated to the project we have
the “program.cs” file in which the code of the program will be developed.
After followed these steps the developer can start to write the code as it is depicted in Figure
2.
Page 7
Visual C#.NET: Console Applications and 2018
Windows Forms
Perhaps the easiest program that we can develop is the classic “Hello World”.
using System;
namespace HelloWorld
{
class Hello
{
static void Main()
{
Console.WriteLine("Hello World!");
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
It is relevant to emphasize that comments in Visual C# can be placed in the code using two
approaches:
// Line 1
// Line 2
Page 8
Visual C#.NET: Console Applications and 2018
Windows Forms
// Line 3
/*
First Line to Comment
Second Line to Comment
Third Line to Comment
*/
A typical useful situation in a console environment application is to receive arguments from
the command line. This situation is depicted in the example below, where we calculate the
total number of arguments passed in the console.
class TestClass
{
static void Main(string[] args)
{
// Display the number of command line arguments:
System.Console.WriteLine(args.Length);
}
}
Another more advanced example is given below. Args[] is considered an array of strings. The
fist thing to do is to check if the console received arguments. If it is positive, therefore the args
is different of null. In the console all arguments are written. For that, a for loop is adopted for
this purpose.
using System;
class Program
{
static void Main(string[] args)
{
if (args == null)
{
Console.WriteLine("args is null");
}
else
{
Console.Write("args length is ");
Console.WriteLine(args.Length);
Page 9
Visual C#.NET: Console Applications and 2018
Windows Forms
Page 10
Visual C#.NET: Console Applications and 2018
Windows Forms
Windows Forms offers a designer tool in Visual Studio to use and insert controls in a form and
range them as per desired layout, with provision for adding code to handle their events, which
implement user interactions. Every control in Windows Forms application is a concrete
instance of a class. The layout of a control in the Graphical User Interface (GUI) and its
behavior are managed using methods and proprieties. Numerous controls are available such as
text boxes, buttons, fonts, icons, combo boxes, area elements, and other graphic objects.
Some best practices for building Windows Forms applications are recommended below
(Techopedia, 2017):
The first step to create a new Windows Form application is to initiate a new instance of Visual
Studio and choose to create this type of project. This situation is illustrated in Figure 3. The
user must provide the following information:
Page 11
Visual C#.NET: Console Applications and 2018
Windows Forms
After this initial step the user can draw the graphical interface and associate the code to each
object. The typical layout is given in Figure 4.
The toolbox window can be use to draw objects in a Windows Form Application. Most
common graphical objects are buttons, checkboxes, comboboxes, labels, monthcalendars and
textboxes. Each object has proprieties, which can be easily accessed using the mouse
Page 12
Visual C#.NET: Console Applications and 2018
Windows Forms
keyboard. Figure 5 gives an example of a property window, in which we can change several
elements, such as the font, image, text, coordinates, etc.
Like in the console application, here the most basic windows form application is also the
traditional “hello world” program. For that, we will create a basic button and associate a
message box when we press it.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace FormWithButton
{
public class Form1 : Form
{
public Button button1;
public Form1()
{
button1 = new Button();
button1.Size = new Size(40, 40);
Page 13
Visual C#.NET: Console Applications and 2018
Windows Forms
Page 14
Visual C#.NET: Console Applications and 2018
Windows Forms
4. C# syntax
4.1 Variables
Variables are stored in memory and allow keeping data during the program’s execution. The
value of each variable can be changed by the user directly or indirectly through a calculation
process. There are several types of C # variables, some of which are quite complex, so
attention is given at this stage to the most common and basic variable types (Figure 6).
It is also important to look to the size and limits of each variable. This information is provided
in Figure 7.
Page 15
Visual C#.NET: Console Applications and 2018
Windows Forms
The example below provides a small example performing initialization and operations with
variables.
using System;
namespace VariableDefinition {
class Program {
static void Main(string[] args) {
short a;
int b ;
double c;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadLine();
}
}
}
Typically it is common the need to convert one type of data to another type. There are two
ways to do it:
using System;
namespace TypeConversionApplication {
class ExplicitConversion {
static void Main(string[] args) {
double d = 5673.74;
int i;
Page 16
Visual C#.NET: Console Applications and 2018
Windows Forms
ToDouble – converts a number to a double type;
ToInt32 – converts a type to a 32-bit integer;
ToString – converts an element to a string.
using System;
namespace TypeConversionApplication {
class StringConversion {
static void Main(string[] args) {
int i = 75;
float f = 53.005f;
double d = 2345.7652;
Console.WriteLine(i.ToString());
Console.WriteLine(f.ToString());
Console.WriteLine(d.ToString());
Console.ReadKey();
}
}
}
4.2 Operators
Operators are use to process and calculate values. They can also be used for other purposes
like testing conditions or for assignment processes. In general we have the following classes of
operators:
Arithmetic operators;
Relational operators;
Logical operators;
Bitwise operators;
Assignment operators;
Unary operators;
Ternary operators;
Misc operators.
Figure 8 provides a concise overview of the operators that we may found in Visual C#.
Page 17
Visual C#.NET: Console Applications and 2018
Windows Forms
Finally, it is also important to look to the precedence of operators. The associativity specifies if
the operator will be evaluated from left to right or right to left. Most operators are evaluated
from left to right, but there are some exceptions like the equality, ternary or assignment
operators.
A simple example of using logical operators is given below. The first test condition returns True
and the second returns False.
using System;
namespace Operator
{
class LogicalOperator
{
public static void Main(string[] args)
{
bool result;
int firstNumber = 10, secondNumber = 20;
// OR operator
result = (firstNumber == secondNumber) || (firstNumber > 5);
Console.WriteLine(result);
// AND operator
result = (firstNumber == secondNumber) && (firstNumber > 5);
Console.WriteLine(result);
}
}
}
An example of suing the ternary operator is given below. Basically, the ternary operator can be
used to avoid the use of if-else statement. If the name is equal to Sam, then it returns “1”, else
it returns “-1”.
Page 18
Visual C#.NET: Console Applications and 2018
Windows Forms
using System;
class Program
{
static void Main()
{
Console.WriteLine(GetValue("Sam"));
Console.WriteLine(GetValue("Tom"));
}
static int GetValue(string name)
{
return name == "Sam" ? 1 : -1;
}
}
using System;
public static void Main(string[] args)
{
int num = 11;
if (num % 2 == 0)
{
Console.WriteLine("It is even number");
}
else
{
Console.WriteLine("It is odd number");
}
}
“Else-if” statement can also be used together like in the below example.
if (i > j)
{
Console.WriteLine("i is greater than j");
}
Page 19
Visual C#.NET: Console Applications and 2018
Windows Forms
else if (i < j)
{
Console.WriteLine("i is less than j");
}
else
{
Console.WriteLine("i is equal to j");
}
}
switch (num)
{
case 10: Console.WriteLine("It is 10"); break;
case 20: Console.WriteLine("It is 20"); break;
case 30: Console.WriteLine("It is 30"); break;
default: Console.WriteLine("Not 10, 20 or 30"); break;
}
}
using System;
public class ForExample
{
public static void Main(string[] args)
{
for(int i=1;i<=10;i++){
Console.WriteLine(i);
Page 20
Visual C#.NET: Console Applications and 2018
Windows Forms
}
}
}
It is possible to create a for loop without limits, like it is shown in the example below.
using System;
namespace Loops {
class Program {
static void Main(string[] args) {
for (; ; ) {
Console.WriteLine("Hey! I am Trapped");
}
}
}
}
Using System;
public class WhileExample
{
public static void Main(string[] args)
{
int i=1;
while(i<=10)
{
Console.WriteLine(i);
i++;
}
}
}
We can use the break instruction to break loop or switch statement. An example is given
below. The numbers are written from 0 to 10. The break is used to exit from the while loop
when i is higher than 10.
int i = 0;
while (true)
{
Page 21
Visual C#.NET: Console Applications and 2018
Windows Forms
4.7 Arrays
Arrays are objects that have the same type and have contiguous memory location. In Visual C#
the array index starts from zero. Arrays offer the following features:
Code optimization;
Random access;
Easy to manipulate, traverse and sort data.
Single dimensional array is the most common and easiest way to have an array. It can be
created by declaring:
This declaration means that we have a single dimensional array composed of five elements. A
more complete example is given below. In this example we have generally three phases: (i)
creating array; (ii) initializing array; and (iii) traversing array. Position 1 and 3 of the array are
not initialized. A for loop is used to traversing the array.
using System;
public class ArrayExample
Page 22
Visual C#.NET: Console Applications and 2018
Windows Forms
{
public static void Main(string[] args)
{
int[] arr = new int[5];//creating array
arr[0] = 10;//initializing array
arr[2] = 20;
arr[4] = 30;
//traversing array
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
}
Foreach loop can also be used to traversing the elements of an array. An example is given
below.
using System;
public class ArrayExample
{
public static void Main(string[] args)
{
int[] arr = { 10, 20, 30, 40, 50 };//creating and initializing array
//traversing array
foreach (int i in arr)
{
Console.WriteLine(i);
}
}
}
We can also have multidimensional arrays that basically have more than one dimension. An
example is given below. In the beginning of the process the array is initialized. Then, each
element is written in the console using two for loops.
using System;
namespace ArrayApplication {
static void Main(string[] args) {
/* an array with 5 rows and 2 columns*/
Page 23
Visual C#.NET: Console Applications and 2018
Windows Forms
using System;
class Program
{
static void Main()
{
// Declare local jagged array with 3 rows.
int[][] jagged = new int[3][];
// Create a new array in the jagged array, and assign it.
jagged[0] = new int[2];
jagged[0][0] = 1;
jagged[0][1] = 2;
// Set second row, initialized to zero.
jagged[1] = new int[1];
// Set third row, using array initializer.
jagged[2] = new int[3] { 3, 4, 5 };
// Print out all elements in the jagged array.
for (int i = 0; i < jagged.Length; i++)
{
int[] innerArray = jagged[i];
for (int a = 0; a < innerArray.Length; a++)
Page 24
Visual C#.NET: Console Applications and 2018
Windows Forms
{
Console.Write(innerArray[a] + " ");
}
Console.WriteLine();
}
}
}
IsFixedSize – it is used to get a value indicating whether the array has a fixed size or not;
Length – it is used to get the total number of elements in all the dimensions of the array;
Rank – it is used to get the number of dimensions of the array.
Below we may find an example of using several operation of an array class. The results are
written in the console using the PrintArray() method.
using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Creating an array
int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
// Creating an empty array
int[] arr2 = new int[6];
// Displaying length of array
Page 25
Visual C#.NET: Console Applications and 2018
Windows Forms
Console.WriteLine("length of first array: "+arr.Length);
// Sorting array
Array.Sort(arr);
Console.Write("First array elements: ");
// Displaying sorted array
PrintArray(arr);
// Finding index of an array element
Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));
// Coping first array to empty array
Array.Copy(arr, arr2, arr.Length);
Console.Write("Second array elements: ");
// Displaying second array
PrintArray(arr2);
Array.Reverse(arr);
Console.Write("\nFirst Array elements in reverse order: ");
PrintArray(arr);
}
// User defined method for iterating array elements
static void PrintArray(int[] arr)
{
foreach (Object elem in arr)
{
Console.Write(elem+" ");
}
}
}
}
4.9 Functions
A function is used to execute statements within a specific block. It has fundamentally two
purposes: (i) increase the readability of the code; and (ii) help in reusing code. When declaring
a new function, three kinds of elements must be defined:
In the first example below we defined a function that uses a string parameter and returns also
a string. Two strings are written in the console: (i) one inside the show() function; and (ii)
another inside the main() function.
using System;
namespace FunctionExample
{
class Program
{
// User defined function
public string Show(string message)
{
Page 26
Visual C#.NET: Console Applications and 2018
Windows Forms
Console.WriteLine("Inside Show Function");
return message;
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
Program program = new Program();
string message = program.Show("My Name");
Console.WriteLine("Hello "+message);
}
}
}
A function can also receive “out” parameters. It works like a reference-type, except that it
does not require variable to initialize before passing. An example is given below. The values
before calling the show() function are equal to 50 and 100; after executing the function, the
new variables are equal to 25 for both variables.
using System;
namespace OutParameter
{
class Program
{
// User defined function
public void Show(out int a, out int b) // Out parameter
{
int square = 5;
a = square;
b = square;
// Manipulating value
a *= a;
b *= b;
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val1 = 50, val2 = 100;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before passing \n val1 = " + val1+" \n val2 = "+val2);
program.Show(out val1, out val2); // Passing out argument
Page 27
Visual C#.NET: Console Applications and 2018
Windows Forms
Console.WriteLine("Value after passing \n val1 = " + val1 + " \n val2 = " + val2);
}
}
}
4.10 Class
A class is the grouping of objects with the same data structure defined by attributes or
properties and operations. In short, classes are descriptions of objects. An example of using a
class only with attributes is provided below. We create a new Student class with two
attributes: (i) id; and (ii) name. Both attributes are written in the console.
using System;
public class Student
{
int id;
String name;
public static void Main(string[] args)
{
Student s1 = new Student();//creating an object of Student
s1.id = 101;
s1.name = "My name";
Console.WriteLine(s1.id);
Console.WriteLine(s1.name);
}
}
A more complete example is given below where we have an example with a class that also
offers two methods. The insert() method is used to add a new id and name. The display()
method is used to write the name of the student in the console. Two new students’ objects are
created in the main function.
using System;
public class Student
{
public int id;
public String name;
public void insert(int i, String n)
Page 28
Visual C#.NET: Console Applications and 2018
Windows Forms
{
id = i;
name = n;
}
public void display()
{
Console.WriteLine(id + " " + name);
}
}
class TestStudent{
public static void Main(string[] args)
{
Student s1 = new Student();
Student s2 = new Student();
s1.insert(101, "Peter");
s2.insert(102, "Tom");
s1.display();
s2.display();
}
}
The “this” syntax can be used to refer to the current instance of the class. This approach can
be used to refer current class instance variable, to pass current object as a parameter to
another method and to declare indexers. A simple example how to adopt this approach is
given below. In this case we create a new Employee() class that contains information on id,
name, and salary.
using System;
public class Employee
{
public int id;
public String name;
public float salary;
public Employee(int id, String name, float salary)
{
this.id = id;
Page 29
Visual C#.NET: Console Applications and 2018
Windows Forms
this.name = name;
this.salary = salary;
}
public void display()
{
Console.WriteLine(id + " " + name+" "+salary);
}
}
class TestEmployee{
public static void Main(string[] args)
{
Employee e1 = new Employee(101, "Peter", 50000f);
Employee e2 = new Employee(102, "Tom", 32000f);
e1.display();
e2.display();
}
}
It is possible to have static classes, which are normal classes but they can’t be instantiated.
Additionally, it can have only static members. Static classes contain only static members,
cannot be instantiated, and cannot contain instance constructors. An example is given below.
There is a static attribute called “PI” and a static method entitled “cube” that receives an
integer parameter.
using System;
public static class MyMath
{
public static float PI=3.14f;
public static int cube(int n){return n*n*n;}
}
class TestMyMath{
public static void Main(string[] args)
{
Console.WriteLine("Value of PI is: "+MyMath.PI);
Console.WriteLine("Cube of 3 is: " + MyMath.cube(3));
}
Page 30
Visual C#.NET: Console Applications and 2018
Windows Forms
}
A class offer inheritance that is a process in which one object acquires all the properties of its
parent object automatically. The class which inherits the members of another class is called
derived class and the class whose members are inherited is called base class. The biggest
advantage of inheritance is to improve the code reusability. An example is given below. A
programmer is a particular case of an employee. It has access to the generic salary of the
employee and it has access to the bonus that is specific of the Programmer class. The
information regarding salary and bonus is written in the console.
using System;
public class Employee
{
public float salary = 40000;
}
public class Programmer: Employee
{
public float bonus = 10000;
}
class TestInheritance{
public static void Main(string[] args)
{
Programmer p1 = new Programmer();
Console.WriteLine("Salary: " + p1.salary);
Console.WriteLine("Bonus: " + p1.bonus);
}
}
4.11 Structs
A struct is a special variable that contains several other variables internally generally of
different types. The internal variables contained by the struct are called members of the struct.
The purpose of a struct is to allow, when storing the data of the same entity, this can be done
with a single variable. In the example below we create a new rectangle structure. A rectangle is
composed of width and height. The area of rectangle is calculated multiplying the width with
height.
using System;
public struct Rectangle
Page 31
Visual C#.NET: Console Applications and 2018
Windows Forms
{
public int width, height;
}
public class TestStructs
{
public static void Main()
{
Rectangle r = new Rectangle();
r.width = 4;
r.height = 5;
Console.WriteLine("Area of Rectangle is: " + (r.width * r.height));
}
}
Another example of using structs is given below. In this case we create a new struct for a book
containing information on book_id, title, author and subject. Two books are created, specified
and printed.
using System;
struct Books {
public string title;
public string author;
public string subject;
public int book_id;
};
public class testStructure {
public static void Main(string[] args) {
Books Book1; /* Declare Book1 of type Book */
Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
Book1.title = "C Programming";
Book1.author = "Nuha Ali";
Book1.subject = "C Programming Tutorial";
Book1.book_id = 6495407;
/* book 2 specification */
Page 32
Visual C#.NET: Console Applications and 2018
Windows Forms
4.12 Strings
In programming a string is a sequence of characters generally used to represent words,
phrases or texts in a program. The example below shows two ways to represent a string
object. “S1” is declared initially as a string and “ch” is declared as an array of char objects.
Then, we create a new string “s2” that is composed of “ch”.
using System;
public class StringExample
{
public static void Main(string[] args)
{
string s1 = "hello";
char[] ch = { 'c', 's', 'h', 'a', 'r', 'p' };
string s2 = new string(ch);
Console.WriteLine(s1);
Console.WriteLine(s2);
}
Page 33
Visual C#.NET: Console Applications and 2018
Windows Forms
}
The string class offers some useful methods to perform the following operations:
The above list is not exaustive, but intends to give an overview about the main functions
offered by String object.
An example of using the IndexOf() method is given below. Basically it returns a value different
of -1 if the “dog” name is found in the string.
using System;
class Program
{
static void Main()
Page 34
Visual C#.NET: Console Applications and 2018
Windows Forms
{
// The input string.
const string value = "The dog is here.";
// Test with IndexOf method.
if (value.IndexOf("dog") != -1)
{
Console.WriteLine("string contains dog!");
}
}
}
In the example below we use the split() method to divide a string in several words.
using System;
class Program
{
static void Main()
{
string s = "there is a cat and a dog";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
}
}
4.13 Exceptions
An exception is an indication that some type of exceptional condition occurred during the
execution of the program. Therefore, exceptions are associated with error conditions that
could not be verified during program compilation. The two activities associated with handling
an exception are:
Page 35
Visual C#.NET: Console Applications and 2018
Windows Forms
Capture – the handling (treatment) of the exceptional situation where the actions
required for the recovery of the error situation are defined.
For each exception that can occur during code execution, a block of handling actions (an
exception handler) must be specified. The structure of exceptions handling is given in Figure
10. The goal is each block is the following:
Two categories of exceptions exist: (i) exceptions that are generated by the application; and (ii)
those generated by the runtime. All exceptions the derived from System.Exception class like
indicated in Figure 11.
Page 36
Visual C#.NET: Console Applications and 2018
Windows Forms
In the example below we provide a simple example how to use the try/catch statement. The
application will launch an exception saying “System.DivideByZeroException: Attempted to
divide by zero”.
using System;
public class ExExample
{
public static void Main(string[] args)
{
try
{
int a = 10;
int b = 0;
int x = a / b;
}
catch (Exception e) { Console.WriteLine(e); }
finally { Console.WriteLine("Finally block is executed"); }
Console.WriteLine("Rest of the code");
Page 37
Visual C#.NET: Console Applications and 2018
Windows Forms
}
}
It is also possible to create user-defined exceptions. It is used to personalize exceptions
according to programming needs. To do this, we need to inherit Exception class. In the
example below a custom exception was created to guarantee that all ages are greater than 18.
using System;
public class InvalidAgeException : Exception
{
public InvalidAgeException(String message)
: base(message)
{
}
}
public class TestUserDefinedException
{
static void validate(int age)
{
if (age < 18)
{
throw new InvalidAgeException("Sorry, Age must be greater than 18");
}
}
public static void Main(string[] args)
{
try
{
validate(12);
}
catch (InvalidAgeException e) { Console.WriteLine(e); }
Console.WriteLine("Rest of the code");
}
}
4.14 Files
Working with files can be done in C# using the FileStream class. This class can be used to
perform synchronous and asynchronous read and write operations. In the example below we
used the ReadByte() function to write in the console the contents of a file. In the end the file is
closed using the close() method.
using System;
using System.IO;
public class FileStreamExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("c:\\test_example.txt", FileMode.OpenOrCreate);
Page 38
Visual C#.NET: Console Applications and 2018
Windows Forms
int i = 0;
while ((i = f.ReadByte()) != -1)
{
Console.Write((char)i);
}
f.Close();
}
}
Other alternative to read information from a file is to use the StreamReader class. It provides
two methods to read data:
An example of using the StreamReader class is given below. In this example all lines of the file
are read and written in the console.
using System;
using System.IO;
public class StreamReaderExample
{
public static void Main(string[] args)
{
FileStream f = new FileStream("c:\example_file.txt", FileMode.OpenOrCreate);
StreamReader s = new StreamReader(f);
using System;
using System.IO;
public class StreamWriterExample
{
public static void Main(string[] args)
{
Page 39
Visual C#.NET: Console Applications and 2018
Windows Forms
CreationTime – it is used to get or set the creation time of the current file;
DirectoryName – it is used to get a string representing the directory’s full path;
Exists – it is used to indicate whether a file exists;
LastAccessTime – it is used to get or set the time from the current file;
Length – it is used to get the size of the current file;
Name – it is used to get the name of the file.
In the example below we use FileInfo class to create a new file and add text to it.
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
try
Page 40
Visual C#.NET: Console Applications and 2018
Windows Forms
{
// Specifying file location
string loc = "C:\\test.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Creating an file instance to write
StreamWriter sw = file.CreateText();
// Writing to the file
sw.WriteLine("This text is written to the file by using StreamWriter class.");
sw.Close();
}catch(IOException e)
{
Console.WriteLine("Something went wrong: "+e);
}
}
}
}
4.15 Collections
Collections are generally complex data structures that can store objects. On the contrary of
arrays, which have size limit, collections can grow or shrink dynamically. There are several
types of collections, such as lists, stacks, queues, dictionaries or hashsets. In the example
below we use a list to store elements. The add() method is used to insert new elements in the
list.
using System;
using System.Collections.Generic;
public class ListExample
{
public static void Main(string[] args)
{
// Create a list of strings
var names = new List<string>();
names.Add("Tom");
names.Add("Lewis");
names.Add("Peter");
Page 41
Visual C#.NET: Console Applications and 2018
Windows Forms
names.Add("Irfan");
// Iterate list element using foreach loop
foreach (var name in names)
{
Console.WriteLine(name);
}
}
}
Other alternative is to use dictionaries that explore the concept of hashtables. It stores values
on the basis of an unique key. It can be used to easily search or remove elements. In the
example below we create a new dictionary and we associate three elements to it.
using System;
using System.Collections.Generic;
public class DictionaryExample
{
public static void Main(string[] args)
{
Dictionary<string, string> names = new Dictionary<string, string>();
names.Add("1","Tom");
names.Add("2","Peter");
names.Add("3","James");
foreach (KeyValuePair<string, string> kv in names)
{
Console.WriteLine(kv.Key+" "+kv.Value);
}
}
}
4.16 Databases
A very useful functionality of C# is the establishment of physical communication with an
external relational database. In this book, we will provide a brief overview how to do it with a
SQL Server Database.
The first step is to define a new SqlConnection instance that takes a connection string as
argument.
Page 42
Visual C#.NET: Console Applications and 2018
Windows Forms
connetionString="Data Source=ServerName; Initial Catalog=DatabaseName;User
ID=UserName;Password=Password"
In a named instance of SQL Server it is also necessary to specify the server location.
When the connection is established, SQL Commands will execute with the help of the
Connection Object and retrieve or manipulate the data in the database. Once the Database
activities are over, Connection should be closed and release the Data Source resources.
A complete example how to establish a connection to a SQL Server Database is given below.
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Data Source=ServerName;Initial
Catalog=DatabaseName;User ID=UserName;Password=Password"
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Cannot open connection ! ");
Page 43
Visual C#.NET: Console Applications and 2018
Windows Forms
}
}
}
}
Once connected to the database, we can execute the set of SQL commands.
Parameterzining the query is done by using the SqlParameter passed into the command. It is a
good approach to avoid SQL Injection.
4.17 XML
XML can be used in information systems to share data. In C# it becomes possible to create,
read and edit content of an XML document. We start by presenting the following example of
an XML file.
Page 44
Visual C#.NET: Console Applications and 2018
Windows Forms
Page 45
Visual C#.NET: Console Applications and 2018
Windows Forms
}
Finally, the last piece of code can be used to edit an element in a XML file. The element is
edited and saved on the same XML file.
Page 46
Visual C#.NET: Console Applications and 2018
Windows Forms
new XElement("Student",
new XAttribute("Id","1"),
new XElement("Name", "Paul"),
new XElement("Email", "[email protected]"),
new XElement("City", "Dallas")
)
)
);
document1.Save(@"Student1.xml");
Page 47
Visual C#.NET: Console Applications and 2018
Windows Forms
5. Scenarios
All these elements are defined in the main function. Two elements are calculated and written
in the console: (i) the value of the price with discount; and (ii) the final price of the product.
namespace Exercicio1
{
class Program
{
static void Main(string[] args)
{
double total_desconto;
double iva = 0.23;
double preço_base = 20;
double desconto = 0.10;
double total;
Console.ReadLine();
}
}
}
using System;
public class PrimeNumberExample
Page 48
Visual C#.NET: Console Applications and 2018
Windows Forms
{
public static void Main(string[] args)
{
int n, i, m=0, flag=0;
Console.Write("Enter the Number to check Prime: ");
n = int.Parse(Console.ReadLine());
m=n/2;
for(i = 2; i <= m; i++)
{
if(n % i == 0)
{
Console.Write("Number is not Prime.");
flag=1;
break;
}
}
if (flag==0)
Console.Write("Number is Prime.");
}
}
using System;
public class SumExample
{
public static void Main(string[] args)
{
int n,sum=0,m;
Console.Write("Enter a number: ");
n= int.Parse(Console.ReadLine());
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
Console.Write("Sum is= "+sum);
}
}
Page 49
Visual C#.NET: Console Applications and 2018
Windows Forms
5.4 Console Application: Sum elements of an array
The idea of this program is to sum five elements of an array. The program offers a Sum()
function that receives an array as argument. Using a for loop this function sums all elements of
an array object. In the main function the program starts by requesting to the user to give five
elements to be added to an array object. After that, the program class the Sum() function and
writes the sum on the console.
using System;
namespace Exercicio3f
{
class Program
{
public static int Sum(int[] arr1)
{
int tot = 0;
for (int i = 0; i < arr1.Length; i++)
tot += arr1[i];
return tot;
}
public static void Main()
{
int[] arr1 = new int[5];
Console.WriteLine("Calcular soma dos elementos de um array");
Page 50
Visual C#.NET: Console Applications and 2018
Windows Forms
The user should start to initialize the elements in the combobox. Three customers are added
by pressing the “initialize” button. Then the user can add or remove items. A new name for the
customer must be provided to add a new item. If the user wants to remove an item, then the
name of customer must be selected in the combobox. In all situations the total number of
items is updated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Exercicio2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Page 51
Visual C#.NET: Console Applications and 2018
Windows Forms
}
}
}
Page 52
Visual C#.NET: Console Applications and 2018
Windows Forms
The program is composed of three phases:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Exercicio3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Comparar strings
if (String.Compare(textBox2.Text, textBox3.Text) == 0)
{
textBox1.AppendText("The strings are equal." + Environment.NewLine);
}
else if (textBox2.Text.Contains(textBox3.Text))
{
textBox1.AppendText("String2 is contained in String1." + Environment.NewLine);
}
else
{
textBox1.AppendText("The strings are different." + Environment.NewLine);
}
Page 53
Visual C#.NET: Console Applications and 2018
Windows Forms
}
}
There are several alternatives to query data from a database. The first thing to consider is if
the query will return just one or multiple values. If the query returns just one value, then the
best approach is to use the “ExceuteScalar()” function. If it returns multiple values, there are
two good solutions: (i) use a dataset and a SQLDataAdapter to return all the data directly to
the dataset; or (ii) use a SQLDataReader, which implies that the connection to the database
remains opened until there is data to be read. Both approaches are presented in the code, but
the second approach is commented.
Page 54
Visual C#.NET: Console Applications and 2018
Windows Forms
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace Exercicio5
{
public partial class Form1 : Form
{
System.Data.SqlClient.SqlConnection con;
public Form1()
{
InitializeComponent();
}
try
{
con.Open();
}
catch (SqlException)
{
//MessageBox.Show("Erro de ligação à BD.");
MessageBox.Show("Erro de ligação à BD", "Erro", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
Page 55
Visual C#.NET: Console Applications and 2018
Windows Forms
}
MessageBox.Show("Database Open");
}
MessageBox.Show("Database Close");
}
textBox1.AppendText(count.ToString() + "\n");
Page 56
Visual C#.NET: Console Applications and 2018
Windows Forms
string v_cidade = "Porto";
string sql_content = @"INSERT into Empresa (id, nome, cidade) VALUES (@sql_id,
@sql_nome, @sql_cidade)";
command.CommandType = CommandType.Text;
command.CommandText = sql_content;
//Avoid SQL Injection
command.Parameters.Add(new SqlParameter("sql_id", v_id));
command.Parameters.Add(new SqlParameter("sql_nome", v_nome));
command.Parameters.Add(new SqlParameter("sql_cidade", v_cidade));
command.CommandType = CommandType.Text;
command.CommandText = sql_content;
command.Parameters.Add(new SqlParameter("sql_id", v_id));
Page 57
Visual C#.NET: Console Applications and 2018
Windows Forms
//Writing data
var writer = new StringWriter();
dst.WriteXml(writer);
textBox1.AppendText(writer.ToString());
}
}
}
Four buttons – these buttons are used to: (i) list customers from a XML file; (ii) add a
new customer to a XML file; (iii) delete a customer from a XML file; (iv) create a new file
only with the customers that present a negative profit;
Input data – it can be used by the user to write the code and name of a new customer.
The customer to be deleted must also be equal to the code given in the textbox;
Area field – write the appropriate messages to the customer, indicating which operation
has been executed.
Page 58
Visual C#.NET: Console Applications and 2018
Windows Forms
It is also relevant to present the structure of “customer.xml” file. Each customer has
information regarding his/her code, name and profit. The profit can be positive or negative. In
the file below we have two customers with negative profit.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
Page 59
Visual C#.NET: Console Applications and 2018
Windows Forms
using System.IO;
using System.Xml.Linq;
namespace Exercicio5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Page 60
Visual C#.NET: Console Applications and 2018
Windows Forms
textBox1.AppendText("Customer deleted. \n");
}
if (c.profit < 0)
{
XElement x2 = new XElement("customer");
x2.Add(new XAttribute("cod", c.cod.ToString()));
x2.Add(new XAttribute("name", c.name));
x2.Add(new XAttribute("profit", c.profit.ToString()));
class Customer
{
public int cod;
public string name;
public float profit;
}
}
}
Page 61
Visual C#.NET: Console Applications and 2018
Windows Forms
Bibliography
Albahari, J. (2017). C# 7.0 in a Nutshell: The Definitive Reference. O'Reilly Media.
Arora, A. (2015, 01 13). Using Await in Catch and Finally Blocks: A New Feature of C# 6.0.
Retrieved from https://www.c-sharpcorner.com/UploadFile/16101a/using-await-in-catch-and-
finally-block-a-new-feature-of-C-Sharp/
Byahut, J. (2014, 06 23). Create Xml File Using Linq to Xml. Retrieved from ASP Helps:
http://www.asphelps.com/Linq/Create-xml.aspx
Home & Learn. (2018, 07 12). Visual C# .NET - Contents Page. Retrieved from
https://www.homeandlearn.co.uk/csharp/csharp.html
Kanjilal, J. (2015, 03 12). Best practices in handling exceptions in C#. Retrieved from
https://www.infoworld.com/article/2896294/application-development/best-practices-in-
handling-exceptions-in-c.html
Rodrigues, J. (2018, 07 12). Manipulando arquivos XML em C#. Retrieved from Linha de Codigo:
http://www.linhadecodigo.com.br/artigo/3449/manipulando-arquivos-xml-em-csharp.aspx
Troelsen, A., & Japikse, P. (2015). C# 6.0 and the .NET 4.6 Framework. Apress.
Webber, Z. (2018). C#: The Utmost Intermediate Course Guide In Fundamentals And Concept Of
C# Programming. Amazon Digital Services LLC.
Page 62