–
The
language of choice in the .Net environment
–
Combines
•
the
power and efficiency of C++
•
simple
and clean OO design of Java
•
language
simplification of VB.
2.
C#
Basic Syntax Rules
•
C#
is case-sensitive.(‘c’ !=‘C’)
•
Each
statement ends with a semicolon.
•
To
group statement, use curly braces.
•
To
tell C# that the statement is a comment, use either of these symbols.
//
single line comment
/* multiline comments*/
•
More
rules as we go along.J
3.
C#
Console Program Structure
using
<namespace>;
namespace
<name>
{
class <name>
{
static void
Main(String [] args)
{
//statement(s)
}
}
}
Example:
using
System; //Console.WriteLine ( ) is found in this namespace.
namespace
HelloWorld //classify each project in a solution.
{
class Program //classify each object in a
namespace
{
static void
Main(String [ ] args)//entry point of the program
{
Console.WriteLine(“Hello
World”); //display Hello World
}
}
}
4.
C#
File Structure
5.
C#
Commonly Used Data Types
–
Numeric
Data Types
•
short
(-32,768 to 32,767)
•
int
(-2,147,483,647 to 2,147,483,647)
•
long
(-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
•
float
(+/-1.5 * 10-45 to approx.+/-3.4 * 1038)
•
decimal
(28 digits. Requires the suffix "m" or "M.“)
•
double
(+/-5.0 * 10-324 to approx. +/-1.7 * 10308)
–
String/Character
Data Types
•
char
(holds a single character)
•
string
(hold string of characters)
–
Boolean/Logical
Data Type
•
bool
(true/false)
6. Variable Declaration
examples
– int num;
–
char middleInitial;
–
string firstName;
–
bool found;
–
double salary;
–
7. Accept Input and Display output
using System;
namespace Profile
{
class Program
{
static void Main(String[] args)
{
string name;
int age;
float weight;
Console.Write("Input
name:");
name = Console.ReadLine();//(By
default,C# read input as string) read input and assign it to name
Console.Write("Input
Age:");
age = int.Parse(Console.ReadLine());
//Read input, convert it to int, assign it to age.
Console.Write("Input Weight in kgs.:");
weight = float.Parse(Console.ReadLine());//Read input, convert it to float, assign it to weight.
//{n}-refers to the placement of the variable separated by
comma.
//{0}-name {1}-age {2:N2}-weight in two decimal places.
Console.WriteLine("You
are {0},your age is {1} and you are {2:N2} kgs.", name, age,
weight);
}
}
}
No comments:
Post a Comment