Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Windows Programming

March 2018
C# Fundamentals
Topics

• Brief History
• Introduction to C#
– Sample C# Program
Introduction to C#

• One of the C family languages


• Written specifically for .NET
• Designed to compete with Java
• Fully object-oriented
• Draws on
– C++
– Java
– VB 6
Introduction to C#

Java
-C++ VB 6
(classes, libraries, security) (structures, preprocessor) (component model)

C#
Introduction to C#

Applications You Can Write with C#


• Windows applications
• Web applications
• Web services …
Introduction to C#

• Structure of C# Program
using System; //Namespace declaration
Class Program
{
Static void Main()
{
Console.WriteLine("Hello C#");
}
}
Cont’d

Namespace
• C# allows code to be placed in
namespaces
• allows the same class names to be used as
long as they are in separate name spaces
• Class names are qualified by the
namespace
Cont’d

Namespace
• using, tells the compiler to search a
namespace for unqualified names
– Using System;
• Indicates you are using the System
namespace
• Can be removed as a cost of redundancy
Cont’d

Comments
//
 Everything to EOL is a comment
/* … */
 Can comment part of a line
 Can comment multiple lines
 Can include // comments
///
Cont’d

Main method
 Main is the starting point for the program
 Every program must have one and only one Main
 Main must be a static method in a class
Static void Main()
{
Console.WriteLine("Hello C#");
}
 writes a line of text on the console
Cont’d

Reading and Writing to Console


• Write on to the console in two ways
– Concatenation
– Place holder syntax
• WriteLine, ReadLine methods of the
Console class
Cont’d

Types
• C# is a strongly typed language
– All variables must have their type declared
• Types are could be
– Intrinsic or built-in types
– User-defined types
Cont’d
Cont’d

-
Cont’d

Literals
• Double is the default for floating point literals
• To get a float, use the f suffix
– float afloat = 53.7f;
• Characters are in single quotes
• Strings are in double quotes
• Unicode characters can be entered via the escape \
unnnn
Cont’d

Literals
• Integer literals
– decimal, octal, or hexadecimal constant
– prefix specifies the base or radix
– suffix for unsigned and long
• Floating point literals
– integer part, a decimal point, a fractional part, and an
exponent part
Cont’d

Literals
• Character literals
– plain character, escape sequence, universal
character
• String literals
Cont’d

• \’ single quote
• \” double quote
• \\ backslash
• \0 null
• \a alert
• \b backspace
• \f form feed
• \n newline
• \r carriage return
• \t horizontal tab
• \v vertical tab
Cont’d

Verbatim Literals
• Treat escape sequences, EOL, … as regular
printable character
• Precede the string with @
• Improves code readability
Cont’d

Type Conversion
• two types
– Implicit
• Compiler does it automatically
• Guaranteed not to lose information
– Explicit
• Happens when the programmer uses a cast
• Type cast operator or Convert class

You might also like