C Basics
C Basics
.NET 3.0/3.5
Session Objectives
• What is C#?
• Understand the basic structure of a C# program.
• Obtain a basic familiarization of what a
"Namespace" is.
• Obtain a basic understanding of what a Class is.
• Learn what a Main method does.
• Learn how to obtain command-line input.
• Learn about console input/output (I/O).
Basic Structure of C# Program
// Namespace Declaration
using System;
// Program start class
class WelcomeCSS
{
// Main begins program execution.
static void Main()
{
// Write to console
Console.WriteLine("Welcome to the C# !");
}
}
Getting Command-Line Input
• What is meant by command line arguments?
• How to take command line input?
• How to convert command line input to required
type?
• Structure of Main() method in C#
Interactive via Command Line
• Taking input from user interactively
• Usage of Console.ReadLine()
• Conversion Functions
Object Oriented Programming
Fundamentals
• Class
• Object
• Method
• Attribute
• Abstraction
• Encapsulation
• Polymorphism
• Inheritance
• Differences b/w object based and OO languages
Console Application in VS 2008
• What is Solution (.sln)?
• IntelliSense
• Automatic Syntax checking
• Properties Window
• Solution Explorer
• Server Explorer
Basics
• Variables
• Initialization of Variables
• Scope of Variables
• Scope Clashes for Local Variables
• Scope Clashes for Fields and Local Variables
• Constants
• C# Data Types
▫ Value Types
▫ Reference Types
Integer Types
Name CTS Type Description
sbyte System.Sbyte 8-bit signed integer
short System.Int16 16-bit signed integer
int System.Int32 32-bit signed integer
long System.Int64 64-bit signed integer
byte System.Byte 8-bit unsigned integer
ushort System.UInt16 16-bit unsigned integer
uint System.UInt32 32-bit unsigned integer
ulong System.UInt64 64-bit unsigned integer
Floating-Point Types
Name CTS Type Description
float System.Single 32-bit single-precision
floating point
double System.Double 64-bit double precision
floating point
Decimal Type
Name CTS Type Description
decimal System.Decimal 128-bit high precision
decimal notation
Boolean Type
• bool type can store either true/false.
Character Type
Name CTS Type Description
char System.Char 16-bit Unicode character
Escape Sequences
Escape Sequence Character
\’ Single quotation mark
\” Double quotation mark
\\ Backslash
\0 Null
\a Alert
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Tab character
\v Vertial tab
Predefined Reference Types
Name CTS Type Description
object System.Object The root type
string System.String Unicode character string
Object Type
Methods of Object Type
• Equals()
• GetHashCode()
• GetType()
• ToString()
The string Type
Operators in C#
• Arithmetic +, -, *, /, %
• Logical &, |, ^, ~, &&, ||, !
• Comparison ==, !=, <, >, <=, >=
• String Concatenation +
• Increment and Decrement ++, --
• Bit Shifting (<<, >>)
• Assignment =, +=, -=, /=, %=, ^=, &=, |=, ^=,
<<=, >>=
• Member Access .
Operators in C# (contd…)
• Indexing []
• Casting ( )
• Conditional ?:
• Delegate Concatenation and removal +,-
• Object Creation (new)
• Size information (sizeof, typeof, is, as)
• Overflow exception control (checked,
unchecked)
• Namespace alias qualifier ::
• Operator Shortcuts
checked operator
byte b=255;
checked
{
b++;
}
Console.WriteLine(b.ToString());
• Eg:
int x=10;
if(x is object)
Cosole.WriteLine(“x is an object”);
as operator
• Used to perform explicit type conversions of
reference types.
unsafe
{
Console.WriteLine(sizeof(int));
}
• Eg:
Console.WriteLine(typeof(string));
Type Conversions
• Implicit
• Explicit
Boxing and Unboxing
Flow Control
• Conditional Statements
• Loops
• Jump Statements
Conditional Statements
• if
• if – else
• if – else if – else
• switch
Loops
• while
• do… while
• for
• foreach
Jump Statements
• goto
• break
• continue
• return
Enumerations
• Example
• Switch example with enumeration
Arrays
Namespaces
using Statement
More on Compiling Options
Option Output
/t:exe A console application (default)
/t:library A class library with manifest
/t:module A component without a manifest
/t:winexe A windows application (without a
console window)
Console I/O
• Console.ReadLine()
• Console.WriteLine()
• Console.Read()
• ConsoleWrite()
Using Comments
• Internal Comments
▫ Single-line (//)
▫ Multiline (/* */)
• XML Documentation Comments (///)