Sie sind auf Seite 1von 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 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

Keywords True Ulong Char Using

Keywords Try Unchecked Checked Virtual

Keywords Typeof Unsafe Class Void

Keywords Uint Catch Const While

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

The base type of all types


String type - sequence of Unicode characters 8-bit signed integral type 16-bit signed integral type

object obj = null;


string str = "Mahesh"; sbyte val = 12; short val = 12;

int
long bool char byte

32-bit signed integral type


64-bit signed integral type Boolean type; a bool value is either true or false Character type; a char value is a Unicode character 8-bit unsigned integral type

int val = 12;


long val1 = 12; long val2 = 34L; bool val1 = true; bool val2 = false; char val = 'h'; byte val1 = 12; byte val2 = 34U;

Type ushort uint ulong

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;

float double decimal

Single-precision floating point type Double-precision floating point type

double val1 = 1.23; double val2 = 4.56D; Precise decimal type with 28 significant digits decimal val= 1.23M;

ushort uint ulong

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.4 Skelton 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

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

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 <
> <= >= == != <

Function Less than


Greater than Less than or equal to Greater than or equal to Equality Inequality Less than

Example expr1 < expr2;


expr1 > expr2; expr1 <= expr2; expr1 >= expr2; expr1 == expr2; expr1 != expr2; expr1 < expr2;

* Conditional Operators

Operator !

Function Logical NOT

Example ! expr1

|| &&
?:

Logical OR (short circuit) Logical AND (short circuit)


Conditional

expr1 || expr2; expr1 && expr2;


cond_expr ? expr1 : expr2;

The conditional operator takes the following general form:

expr ? execute_if_expr_is_true : execute_if_expr_is_false;

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:

*How To Use Constant?


class Calendar2{ static void Main(string[] args) { const int months = 12, weeks = 52, days = 365; const double daysPerWeek = days / weeks;

const double daysPerMonth = days / months;


System.Console.WriteLine(daysPerWeek);

System.Console.WriteLine(daysPerMonth);
System.Console.ReadLine(); }

1.7 Type Conversion:


There are two types of type conversion implicit and 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 int to 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 int data 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.

* 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(); } } }

* 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();
} } }

Das könnte Ihnen auch gefallen