1.Intro. to C#.Net
-
Upload
siddharth-mukherjee -
Category
Documents
-
view
222 -
download
0
Transcript of 1.Intro. to C#.Net
-
7/31/2019 1.Intro. to C#.Net
1/20
1.Introduction to C#. Net
1.1 introduction to C#
1.1.1 What is C#?C#(pronounced as C Sharp) is the
language that has been designed from ground up with
Internet in mind . It is modern language That combines
the power of C++ with productivity of VB and eleganceof Java.
1.1.2 Why C#?
C# is a new programming language introduced
with .NET for developers who prefer of C-style syntax or
already be familier with Java or C++., can quickly
implement application and components that support the
.Net framework
-
7/31/2019 1.Intro. to C#.Net
2/20
1.1.3 History of C#?
The history of C# begins in early 1970s when C was
invented in which UNIX was written. But C had some drawbacks.
Then came C++ in 1980s , who like its precedors took the world bystorm.
SUN created JAVA. They were motivated with the World wide Web.
This sought a single environment that would target multiple
platforms. Programs in JAVA are not complied to machine code, but
instead to an intermediate language or byte code that can be
executed on any machine equipped with suitable interpreter
program .The Java provide (The Java Runtime).
Here Comes in C#, developed by Anders Hejlsberg.This is a language
designed for the .Net platform . C# promotes one-stop coding,thegrouping of Classes, interfaces and implementations together in one
file so that developer can edit code more easily
-
7/31/2019 1.Intro. to C#.Net
3/20
1.2 Keywords in C#
Contains rich set of 76 reserved keywords. Keywords
can be used as identifiers prefaced by an @.
Keywords Keywords Keywords Keywords
abstract as base bool
Break Byte Case Catch
Char Checked Class Const
Continue Decimal Default DelegateDo Double Else Enum
Event Explicit Extern False
Finally Fixed Float For
Foreach Goto If Implicit
In Int Interface Internal
Is Lock Long Namespace
New Null Object Operator
Out Override Params Private
Protected Public Readonly Ref
-
7/31/2019 1.Intro. to C#.Net
4/20
Keywords Keywords Keywords Keywords
True Try Typeof Uint
Ulong Unchecked Unsafe Catch
Char Checked Class Const
Using Virtual Void While
Is and typeofare used to find out the type of an object
at run time
-
7/31/2019 1.Intro. to C#.Net
5/20
1.3 Data Types in C#
Type Description Example
object The base type of all types object obj = null;
string String type - sequence of Unicode characters string str = "Mahesh";
sbyte 8-bit signed integral type sbyte val = 12;short 16-bit signed integral type short val = 12;
int 32-bit signed integral type int val = 12;
long 64-bit signed integral type long val1 = 12;
long val2 = 34L;
bool Boolean type; a bool value is either true orfalse
bool val1 = true;bool val2 = false;
char Character type; a char value is a Unicode
character
char val = 'h';
byte 8-bit unsigned integral type byte val1 = 12;
byte val2 = 34U;
Most of the data type in c# are taken from C and C++. This tables lists data
types, their description, and a sample example.
-
7/31/2019 1.Intro. to C#.Net
6/20
Type Description Example
ushort 16-bit unsigned integral type ushort val1 = 12;
ushort val2 = 34U;
uint 32-bit unsigned integral type uint val1 = 12;uint val2 = 34U;
ulong 64-bit unsigned integral type ulong val1 = 12;
ulong val2 = 34U;
ulong val3 = 56L;
ulong val4 =78UL;
float Single-precision floating point type float val = 1.23F;
double Double-precision floating point type double val1 = 1.23;
double val2 = 4.56D;
decimal Precise decimal type with 28 significant digits decimal val= 1.23M;
ushort 16-bit unsigned integral type ushort val1 = 12;
ushort val2 = 34U;uint 32-bit unsigned integral type uint val1 = 12;
uint val2 = 34U;
ulong 64-bit unsigned integral type ulong val1 = 12;
ulong val2 = 34U;
ulong val3 = 56L;
ulong val4 =78UL;
-
7/31/2019 1.Intro. to C#.Net
7/20
1.3.1 Types in C#C# supports two kinds of types: value types and reference types.
Types Description Types Description
Value Types Includes simple datatypes such as int, char,
bool, enums
Value Types Includes simple datatypes such as int, char,
bool, enums
Reference Types Includes object, class,
interface, delegate, and
array types
Reference Types Includes object, class,
interface, delegate, and
array types
*Value Types- Value type objects direct contain the actual data in a variables.
With value types, the variables each have their own copy of the data, and it is not possible
for operations on one to affect the other.
int i = 10;
*ReferenceTypes- Reference type variables stores the reference of the actual data.With reference types, it is possible for two variables to reference the same object, and
thus possible for operations on one variable to affect the object referenced by the other
variable.
MyClass cls1 = new MyClass();
-
7/31/2019 1.Intro. to C#.Net
8/20
1.3.3How can we Declare and assign a value to the Variable:
Declaration:
int a;
string name;
Assignment/Intialization:
A=10;
Name=XYZ;
1.3.2 What is Variable?
Variable is place holder for values in memory or simply variable is
object of data Type.
-
7/31/2019 1.Intro. to C#.Net
9/20
1.4Skelton Of C# Program?
Console Based program
using System;
using System.Collections;
#region entry point
public class EntryClass
{
public static void Main()
{
NewClass t = new NewClass();
}}
#endregion entry point
-
7/31/2019 1.Intro. to C#.Net
10/20
1.4.1 Sample C# program?
Console Based program
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{static void Main(string[] args)
{
System.Console.WriteLine("HELLO WORLD");
System.Console.ReadLine();
}
}
}
OUTPUT:
HELLO WORLD
-
7/31/2019 1.Intro. to C#.Net
11/20
1.5 Operators in C#
Operator Function Example
+ addition 11 + 2
- subtraction 11 - 2* multiplication 11 * 2
/ division 11 / 2
% remainder 11 % 2
++ Increment by 1 ++expr1; expr2++;-- Decrement by 1 --expr1; expr2--;
*Arithmetic Operators:
-
7/31/2019 1.Intro. to C#.Net
12/20
Operator Function Example
< Less than expr1 < expr2;
> Greater than expr1 > expr2;
= expr2;
== Equality expr1 == expr2;
!= Inequality expr1 != expr2;
< Less than expr1 < expr2;
*RelationalOperators
-
7/31/2019 1.Intro. to C#.Net
13/20
Operator Function Example
! Logical NOT ! expr1
|| Logical OR (short circuit) expr1 || expr2;
&& Logical AND (short circuit) expr1 && expr2;
?: Conditionalcond_expr ? expr1 :
expr2;
* Conditional Operators
The conditional operator takes the following general form:
expr
? execute_if_expr_is_true : execute_if_expr_is_false;
-
7/31/2019 1.Intro. to C#.Net
14/20
1.6 Constants :
Classes and structs can declare constants as members.
Constants are values which are known at compile time and donot change. Constants are declared as a field, using the const
keyword before the type of the field. Constants must be
initialized as they are declared.
For example:
Multiple constants of the same type can be declared at the
same time, for example:
http://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.80).aspxhttp://msdn.microsoft.com/en-us/library/e6w8fe1b(VS.80).aspx -
7/31/2019 1.Intro. to C#.Net
15/20
class Calendar2{
static void Main(string[] args)
{
const int months = 12, weeks = 52, days = 365;
constdouble daysPerWeek = days / weeks;
constdouble daysPerMonth = days / months;
System.Console.WriteLine(daysPerWeek);
System.Console.WriteLine(daysPerMonth);
System.Console.ReadLine();
}
}
*How To Use Constant?
-
7/31/2019 1.Intro. to C#.Net
16/20
1.7 Type Conversion:
There are two types of type conversion implicitand explicit.
Implicit: Automatic compiler conversion where data loss is not an issue.
e.g. int iVal = 34;
long lVal = iVal;
Explicit: A conversin where data loss may happen and is recommended that the
programmer writes additional processing
e.g. long lVal = 123456;int iVal = (int) lVal;
A common rule of thumb is that it is much easier to convert up then it is to convert
down. For example, conversion from intto long is an easy operation, but converting the
other way around is not so easy. Remember the long data type is a bigger type then the
intdata type is. To prevent data loss just remember to convert from small to large.
There may be situations where you cannot get around from converting a large data
type to small. This where explicit conversion comes into play.
-
7/31/2019 1.Intro. to C#.Net
17/20
* Csharp Implicit Conversion:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{int x = 2;
double y = 12.2;
double z;
z = x + y; //x is automatically converted into a double
Console.WriteLine(z);
Console.Read();
}
}
}
-
7/31/2019 1.Intro. to C#.Net
18/20
* Csharp Explicit Conversion:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args){
double x = 2.1;
int y = 12;
int z = (int)x + y; //Explicit conversion from double to int
Console.WriteLine(z);Console.Read();
}
}
}
-
7/31/2019 1.Intro. to C#.Net
19/20
* Conversion by Convert Class:
1)
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{ static void Main(string[] args)
{
int y;
y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(y);
Console.Read();
}
}
}
-
7/31/2019 1.Intro. to C#.Net
20/20
* Conversion by Convert Class:
2)
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{ static void Main(string[] args)
{
double lVal = 123.45;
int iVal = Convert.ToInt32(lVal); //double is converted to int
Console.WriteLine(iVal);
Console.Read();
}
}
}