Professional Documents
Culture Documents
Lecture 03
Lecture 03
Techniques
C# Programming with
Microsoft .NET
Recap…
Demo application….
Understanding C#
Fundamental
Structure of a C# Program?
• Program execution begins from static Main( ).
• using system; using keyword refers to
resources in the .NET Framework class library.
• Statements are commands that perform
actions
– A program is made up of many separate statements
– Statements are separated by a semicolon
– Braces are used to group statements
using
using System;
System;
class
class Salam{
Salam{
static
static void
void Main(string[]
Main(string[] args)
args) {{
Console.WriteLine
Console.WriteLine (“Asalam
(“Asalam Alykum!");
Alykum!");
}}
}}
How to Format Code in C#
• Use indentation to indicate enclosing statements
• C# is case sensitive (cAse and case are 2 diff
things)
• White space is ignored
• Single line comments same as in C or C++ by
using //
• Multiple-line comments by using /* and */
• We can use keyword as an identifier if preceded by
@ e.g. @int;
using
using System;
System;
class
class Salam{
Salam{
static
static void
void Main(string[]
Main(string[] args)
args) {{
Console.WriteLine
Console.WriteLine (“Asalam
(“Asalam Alykum!");
Alykum!");
}}
}}
Programming practices
• By convention, always begin a class name’s identifier with
a capital letter and start each subsequent word in the
identifier with a capital letter. e.g. MyFirstClass
• By convention, a file that contains a single public class
should have a name that is identical to the class name
(plus the .cs extension) in both spelling and capitalization.
• Whenever you type an opening left brace, {, in your
application, immediately type the closing right brace, }
this will help in avoiding braces miss match.
• Ctrl + } helps in identifying pairs
• Properly indented code is always admired, VS IDE also
provide auto indentation. Edit->Advanced->Format
Document.
Predefined Datatypes
• Predefined or primitive datatypes
– Predefined types are those provided by
C# and the .NET Framework
– Most are similar as in C++
– string, decimal, Int32, Int64 etc.
– Decimal datatype special for monetary
values
– Why decimal ? Whats wrong with float n
double?
Formatting Text with Console.Write
and Console.WriteLine
// Displaying multiple lines of text with string formatting.
using System;
public class Welcome4
{
// Main method begins execution of C# application
public static void Main( string[] args )
{
Console.WriteLine( "{0}\n{1}", "Welcome to", "C#
Programming!" );
} // end Main
} // end class Welcome4
Welcome to
C# Programming!
How to Declare and Initialize
Strings
• Example string
string s = "Hello World"; // Hello World
• Understanding Unicode
The character “A” is represented by “U+0041”
// Displaying the sum of two numbers input from the keyboard.
using System;
public class Addition{
// Main method begins execution of C# application
public static void Main( string[] args ) {
int number1; // declare first number to add
int number2; // declare second number to add
int sum; // declare sum of number1 and number2
+ DisplayMessage(msg:string)
public string CourseName
{
get
{
return courseName;
} // end get
set
{
courseName = value;
} // end set
} // end property CourseName
} // end class GradeBook
Auto implemented
properties
C# provides auto implementation of properties,