Beruflich Dokumente
Kultur Dokumente
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 elegance of 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
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 by storm. 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,the grouping of Classes, interfaces and implementations together in one file so that developer can edit code more easily
1.2 Keywords in C# Contains rich set of 76 reserved keywords. Keywords can be used as identifiers prefaced by an @. Keywords abstract Break Char Continue Do Keywords as Byte Checked Decimal Double Keywords base Case Class Default Else Keywords bool Catch Const Delegate Enum
Event
Finally Foreach In Is New Out Protected
Explicit
Fixed Goto Int Lock Null Override Public
Extern
Float If Interface Long Object Params Readonly
False
For Implicit Internal Namespace Operator Private Ref
Is and typeof are used to find out the type of an object at run time
1.3 Data Types in C# Most of the data type in c# are taken from C and C++. This tables lists data types, their description, and a sample example.
Type Description Example
object
string sbyte short
int
long bool char byte
Description 16-bit unsigned integral type 32-bit unsigned integral type 64-bit unsigned integral type
Example ushort val1 = 12; ushort val2 = 34U; uint val1 = 12; uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; ulong val4 =78UL; float val = 1.23F;
double val1 = 1.23; double val2 = 4.56D; Precise decimal type with 28 significant digits decimal val= 1.23M;
16-bit unsigned integral type 32-bit unsigned integral type 64-bit unsigned integral type
ushort val1 = 12; ushort val2 = 34U; uint val1 = 12; uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; ulong val4 =78UL;
1.3.1 Types in C#
C# supports two kinds of types: value types and reference types.
Types Value Types Description Types Description Includes simple data types such as int, char, bool, enums Includes object, class, interface, delegate, and array types Includes simple data Value Types types such as int, char, bool, enums Includes object, class, Reference Types interface, delegate, and array types
Reference 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; *Reference Types- 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();
1.3.2 What
is Variable?
Variable is place holder for values in memory or simply variable is object of data Type. 1.3.3How can we Declare and assign a value to the Variable: Declaration: <type> <variable name> int a; string name; Assignment/Intialization: A=10; Name=XYZ;
1.5 Operators in C#
*Arithmetic Operators:
Operator
+
Function
addition
Example
11 + 2
subtraction
multiplication
11 - 2
11 * 2
/
% ++ --
division
remainder Increment by 1 Decrement by 1
11 / 2
11 % 2 ++expr1; expr2++; --expr1; expr2--;
*Relational Operators
Operator <
> <= >= == != <
* Conditional Operators
Operator !
Example ! expr1
|| &&
?:
1.6 Constants : Classes and structs can declare constants as members. Constants are values which are known at compile time and do not 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:
System.Console.WriteLine(daysPerMonth);
System.Console.ReadLine(); }
* 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(); } } }
* 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(); } } }
* 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();
} } }