This document provides information about the C# programming language. It discusses that C# is an object-oriented language that can be used to build a variety of applications like Windows and web. Visual C# .NET is Microsoft's integrated development environment (IDE) for building C# applications and is part of the Visual Studio suite. The document also covers C# language fundamentals like variables, data types, operators, and conditional statements.
Report
Share
Report
Share
1 of 72
More Related Content
unit 1 (1).pptx
2. C# (pronounced "C sharp") is a simple, modern, object-
oriented, and type-safe programming language.
C# combines the high productivity of Rapid
Application Development (RAD) languages and the
raw power of C++
Visual C# .NET is Microsoft's C# development tool
It includes an interactive development environment,
visual designers for building Windows and Web
applications.
3. Visual C# .NET is part of a suite of products,
called Visual Studio .NET, that also includes
Visual Basic .NET, Visual C++ .NET, and the
Jscript scripting language.
All of these languages provide access to the
Microsoft .NET Framework, which includes a
common execution engine and a rich class
library
4. It is a modern, general-purpose
programming language
It is object oriented.
It is component oriented.
It is easy to learn.
It is a structured language.
It produces efficient programs.
It can be compiled on a variety of computer
platforms.
It is a part of .Net Framework.
5. Like c++, both single and multiline comments supports
C# builds on the
◦ High performance [c]
◦ Object oriented structure ( c++)
◦ Security ( java)
◦ Rapid development ( visual basic)
7. The source code for a C# program is typically
stored in one or more text files with a file
extension of .cs, as in hello.cs.
The source code for a C# program is typically
stored in one or more text files with a file
extension of .cs, as in hello.cs.
9. using System- directive references a
namespace called System that is provided by
the Microsoft .NET Framework class library.
10. A variable is nothing but a name given to a
storage area that our programs can
manipulate.
Each variable in C# has a specific type, which
determines the size and layout of the
variable's memory the range of values that
can be stored within that memory and the set
of operations that can be applied to the
variable.
11. Type Example
Integral types sbyte, byte, short, ushort, int, uint,
long, ulong, and char
Floating point
types
float and double
Decimal types decimal
Boolean types true or false values, as assigned
Nullable types Nullable data types
14. using System;
namespace VariableDefinition
{
class Program
{
static void Main(string[] args)
{
short a;
int b ;
double c;
a = 10;
b = 20;
c = a + b;
Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
Console.ReadLine();
}
Output:
a = 10, b = 20, c = 30
15. Example
int num;
num = Convert.ToInt32(Console.ReadLine());
The function Convert.ToInt32() converts the data
entered by the user to int data type,because
Console.ReadLine() accepts the data in string format.
lvalue: An expression that is an lvalue may appear as
either the left-hand or
right-hand side of an assignment.
rvalue: An expression that is an rvalue may appear on
the right- but not lefthand side of an assignment.
Variables are lvalues and hence they may appear on
the left-hand side of an assignment. Numeric literals
are rvalues and hence they may not be assigned and
can not appear on the left-hand side
16. Single-line Comments
Single-line comments start with two forward
slashes (//).
Example
// This is a comment
Console.WriteLine("Hello World!");
C# Multi-line Comments
Multi-line comments start with /* and ends
with */.
17. data type specifies the size and type of
variable values.
Value types
Reference types
Pointer types
18. Data Type Size Description
int 4 bytes Stores whole numbers from -2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7
decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal
digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single quotes
string 2 bytes per
character
Stores a sequence of characters, surrounded by double q
19. Value type variables can be assigned a value
directly
They are derived from the class
System.ValueType.
The value types directly contain data
examples are int, char, and float, which
stores numbers, alphabets, and floating point
numbers, respectively.
The system allocates memory to store the
value
23. Use float or double
The precision of a floating point value
indicates how many digits the value can have
after the decimal point. The precision
of float is only six or seven decimal digits,
while double variables have a precision of
about 15 digits. Therefore it is safer to
use double for most calculations.
25. A floating point number can also be a scientific number with an "e" to
indicate the power of 10:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
float f1 = 35e3F;
double d1 = 12E4D;
Console.WriteLine(f1);
Console.WriteLine(d1);
}
}
}
Output
35000
120000
26. The reference types do not contain the actual
data stored in a variable, but they contain a
reference to the variables.
they refer to a memory location
Using multiple variables, the reference types
can refer to a memory location.
If the data in the memory location is changed
by one of the variables, the other variable
automatically reflects this change in value.
Example- object,dynamic, and string
27. The Object Type is the ultimate base class for all data
types in C# Common Type System (CTS).
The object types can be assigned values of any other
types, value types, reference types, predefined or
userdefined types.
However, before assigning values, it needs type
conversion
When a value type is converted to object type, it is
called boxing
when an object type is converted to a value type, it is
called unboxing
object obj;
obj = 100; // this is boxing
Int b=(int)obj//unboxing
28. We can store any type of value in the dynamic data
type variable.
Type checking for these types of variables takes place
at run-time.
Syntax
dynamic <variable_name> = value;
example
dynamic d = 20;
Dynamic types are similar to object types except that
type checking for object type
variables takes place at compile time, whereas that
for the dynamic type variables takes place at run
time.
29. String Type
The String Type allows you to assign any
string values to a variable
The string type is an alias for the
System.String class.
Example
String str = "Adithya Institute of Technology";
30. Pointer type variables store the memory
address of another type.
Pointers in C# have the same capabilities as
the pointers in C or C++.
Syntax
type* identifier;
example
char* cptr;
int* iptr;
31. Implicit Casting (automatically) - converting a
smaller type to a larger type size
char -> int -> long -> float -> double
Explicit Casting (manually) - converting a
larger type to a smaller size type
double -> float -> long -> int -> char
32. using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to
double
Console.WriteLine(myInt);
Console.WriteLine(myDouble);
}
}
}
33. using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble);
Console.WriteLine(myInt);
}
}
}
35. Operator Name Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 x++
-- Decrement Decreases the value of a variable by 1 x--
36. using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 2;
Console.WriteLine(x+y);
Console.WriteLine(x-y);
Console.WriteLine(x*y);
Console.WriteLine(x/y);
Console.WriteLine(x % y);
Console.WriteLine(x++);
Console.WriteLine(x--);
}
}
}
37. Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
39. using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 3;
Console.WriteLine(x == y);
Console.WriteLine(x != y);
Console.WriteLine(x > y);
Console.WriteLine(x < y);
Console.WriteLine(x >= y);
Console.WriteLine(x <= y);
}
}
}
40. Assume variable A holds Boolean value true
and variable B holds Boolean value false
41. Bitwise operator works on bits and perform
bit by bit operation. The truth tables for &, |,
and ^ are as follows
48. conditional statements
Looping
Jumping
use the if statement
to specify a block of C# code to be executed
if a condition is True.
49. if statement
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
if (20 > 18)
{
Console.WriteLine("20 is greater than 18");
}
}
}
}
Output
20 is greater than 18
50. Use the else statement to specify a block of code to be executed if the
condition is False.
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}Output:
Good evening
51. using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
52. using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 20;
string result = (time < 18) ? "Good day." :
"Good evening.";
Console.WriteLine(result);
}
}
}
53. For loop
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i <= 10; i = i + 2)
{
Console.WriteLine(i);
}
}
}
}
59. The while loop- loops through a block of code as long as a
specified condition is True
Example
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
}
}
}
61. This loop will execute the code block once,
before checking if the condition is true, then
it will repeat the loop as long as the condition
is true.
64. switch statement to select one of many code
blocks to be executed.
how it works?
The switch expression is evaluated once
The value of the expression is compared with
the values of each case
If there is a match, the associated block of
code is executed
65. class Program
{
static void Main(string[] args)
{
int day = 4;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
} } }}
67. break keyword, it breaks out of the switch
block.
This will stop the execution of more code and
case testing inside the block.
68. The break statement can also be used to jump out of a loop.
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
Console.WriteLine(i);
}
}
}
}