C# Part
C# Part
In this session
1. We will learn the basic structure of a c# program. The program we used in this video is shown
below.
// Namespace Declaration
using System;
class Pragim
{
public static void Main()
{
// Write to console
Console.WriteLine ("Welcome to PRAGIM Technologies!");
}
}
2. Understand the purpose of using System declaration - The namespace declaration, using
System, indicates that you are using the System namespace. If you omit the using System,
declaration, then you have to use the fully qualified name of the Console class. A namespace is
used to organize your code and is collection of classes, interfaces, structs, enums and
delegates. We will discuss about namespaces in detail in a later session.
3. Purpose of Main() method - Main method is the entry point into your application.
In this video, we will discuss the different built-in types that are available in c#.
Built-in types in C#
1. Boolean type – Only true or false
2. Integral Types - sbyte, byte, short, ushort, int, uint, long, ulong, char
3. Floating Types – float and double
4. Decimal Types
5. String Type
Escape Sequences in C#
http://msdn.microsoft.com/en-us/library/h21280bw.aspx
Verbatim literals make escape sequences translate as normal printable characters to enhance
readability.
Practical Example:
Without Verbatim Literal : “C:\\Pragim\\DotNet\\Training\\Csharp” – Less Readable
With Verbatim Literal : @“C:\Pragim\DotNet\Training\Csharp” – Better Readable
Verbatim Literal is a string with an @ symbol prefix, as in @“Hello”. Verbatim literals make
escape sequences translate as normal printable characters to enhance readability.
Practical Example:
Without Verbatim Literal : "C:\\Pragim\\DotNet\\Training\\Csharp"; // Less Readable
With Verbatim Literal : @"C:\Pragim\DotNet\Training\Csharp"; // Better Readable
C# example program used in the demo
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// Displaying double quotes in c#
string Name = "\"Pragim\"";
Console.WriteLine(Name);
// C# verbatim literal
Name = @"c:\Pragim\DotNet\Training\Csharp";
Console.WriteLine(Name);
}
}
}
In this video, we will discuss the common operators that are available in c# programming
language.
Assignment Operator =
Arithmetic Operators like +,-,*,/,%
Comparison Operators like ==, !=,>, >=, <, <=
Conditional Operators like &&, ||
Ternary Operator ?:
Null Coalescing Operator ??
The example below is not using the ternary operator. Look at the amount of code we have
to write to check if a number is equal to 10, and then initialise a boolean variable to true or false
depending on whether the number is equal to 10 or not.
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
int number = 10;
bool isNumber10;
if (number == 10)
{
isNumber10 = true;
}
else
{
isNumber10 = false;
}
Ternary operator example : We have rewritten the above program using ternary operator.
Notice the amount of code we have to write is greatly reduced.
using System;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
int number = 10;
By default value types are non nullable. To make them nullable use ?
int i = 0 (i is non nullable, so "i" cannot be set to null, i = null will generate compiler error)
int? j = 0 (j is nullable int, so j=null is legal)
Nullable types bridge the differences between C# types and Database types
if (TicketsOnSale == null)
{
AvailableTickets = 0;
}
else
{
AvailableTickets = (int)TicketsOnSale;
}
Example: Converting an int to a float will not loose any data and no exception will be thrown,
hence an implicit conversion can be done.
Where as when converting a float to an int, we loose the fractional part and also a possibility of
overflow exception. Hence, in this case an explicit conversion is required. For explicit
conversion we can use cast operator or the convert class in c#.
Console.WriteLine(f);
}
}
Console.WriteLine(i);
}
}
2. Parse() method throws an exception if it cannot parse the value, whereas TryParse() returns
a bool indicating whether it succeeded or failed.
3. Use Parse() if you are sure the value will be valid, otherwise use TryParse()
using System;
class Program
{
public static void Main()
{
// Initialize and assign values in different lines
int[] EvenNumbers = new int[3];
EvenNumbers[0] = 0;
EvenNumbers[1] = 2;
EvenNumbers[2] = 4;
Disadvantages: Arrays cannot grow in size once initialized. Have to rely on integral indices to
store or retrieve items from the array.
Part 9 - C# Tutorial - Comments
Suggested Videos
Part 6 - Nullable Types
Part 7 - Datatype Conversions
Part 8 - Arrays
Comments are used to document what the program does and what specific blocks or lines of
code do. C# compiler ignores comments.
Note: Don't try to comment every line of code. Use comments only for blocks or lines of code
that are difficult to understand