Thứ Hai, 3 tháng 3, 2014

Tài liệu C# Corner ppt

C# Corner: C# and .NET Developer's Network




All Source Code
ADO.NET
Exception
Handling
General
GDI+
Internet
Mobiling
Multithreading
Networking
Printing
Security
Strings & Arrays
Windows Forms
WebForms
XML.NET
-
Articles
Effective C#
FAQ
How do I
Learn C#
Tutorials
Source Code
Articles
FAQ
Learn VB.NET
Tutorials
Source Code
Articles
FAQ
Learn ASP.NET
Tutorials
COBOL.NET
Eiffel.NET
FoxPro.NET
JScript .NET
Learn XML
Mobile & SOAP
My Services
Open Source
VBScript .NET
VC++.NET
Visual J#
Visual Studio.NET
Introduction to C#
This part of tutorial explains about C# and how to write and compile your first C# program from
command line.

What is C#?
C# is a new programming language developed by Microsoft. C# has power of C++ since it's
derived from C and C++. It is simpler as VB. Besides that, C# is a Java like language for web
programming and it has some good features of Delphi too. Microsoft says, that C# is the best
language to develop its .NET Framework applications.
Installing .NET SDK
Installing .NET SDK is first step to run C# on your machine. You can install .NET SDK on Windows
ME, Windows NT, or Windows 2000. But Windows 2000 is recommended. After selecting your OS,
you need to follow these steps:
● Install IE 5.5
● Install Microsoft .NET Framework SDK. It's free and you can download it here. NET
Framework SDK.
● After installing these you can write your code in any text editor and save it as .cs
extension. Type this in an notepad and save as my.cs.
C# Compiler and Editors
.NET SDK Beta 1 release of Microsoft's new platform, .NET, incorporated with C# command line
compiler. You must have to install .NET SDK to run a C# program. Once you install .NET SDK,
you can write your C# program in any text editor including notepad, wordpad or Visual Studio.
There are some third party editors are available in the market too. Some of them are free. Check
out
tools section of C# Corner.
Write your first C# program
Writing your first C# program is similar to writing C++ applications. You open any text editor, I
described in the above paragraph and type this code.
using System;
class MyClass
{
static void Main() {
Console.WriteLine("Hello World!");
}
}

Compile your first C# program
Now use command line to compile your cs file. C# compiler takes at least one argument i.e., file
name. Say your C# file name is myclass.cs then here is command line syntax.
csc myclass.cs

C# compiler creates an exe file in the bin dir of your project. Just run this exe
and see the output.

http://www.c-sharpcorner.com/Language/cs_lang_1.asp (1 of 2) [11/27/2001 7:19:46 PM]
C# Corner: C# and .NET Developer's Network
About Us
Books
Book Chapters
Career Center
Discussion
Downloads
Events
Hosting
Links
Magazines
Members
News Letter
Sponsors
Training
Tools
Jobs
Join C# Corner
Our Partners
Submit Code
Win Prizes
Mindcracker
Now, lets take a look of your code line by line.
The first line of your program is using System.
using System.

Why using System? System is a namespace which stores system classes. The
Console class, I used in the program to display the output on the console is
defined in the System namespace. That's why this like is there.
Next line is class MyClass. The class keyword in C# is used to create a new
class.
class MyClass
{


}
Each class has one static void Main() function. This function is the entry point of a C# program.
static void Main() {
Console.WriteLine("Hello, C# World!");
}
The WriteLine method of the Console class writes text to the console.



Mahesh is Admin and the founder of C# Corner. He has been programming in C++, MFC, Visual Basic, COM, ATL,
Database Programming over 5 years.
More articles by Mahesh

contact: webmaster@c-sharpcorner.com
copyright © 2000 c-sharpcorner.com. All rights are reserved. See
terms and condition to use this site and its contents.
Sponsors:
devexpress microgold apress

http://www.c-sharpcorner.com/Language/cs_lang_1.asp (2 of 2) [11/27/2001 7:19:47 PM]
C# Corner: C# and .NET Developer's Network




All Source Code
ADO.NET
Exception
Handling
General
GDI+
Internet
Mobiling
Multithreading
Networking
Printing
Security
Strings & Arrays
Windows Forms
WebForms
XML.NET
-
Articles
Effective C#
FAQ
How do I
Learn C#
Tutorials
Source Code
Articles
FAQ
Learn VB.NET
Tutorials
Source Code
Articles
FAQ
Learn ASP.NET
Tutorials
COBOL.NET
Eiffel.NET
FoxPro.NET
JScript .NET
Learn XML
Mobile & SOAP
My Services
Open Source
VBScript .NET
VC++.NET
Visual J#
Visual Studio.NET
Working with Data Types
First thing a programmer looks for is what kind of data types a programming languages has and how to use
them. In this part, I will cover C# data types and how to use them in a program.

Basic Data Types
Most of the data type in c# are taken from C and C++. This tables lists data types, their description, and a
sample example.
Data 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 or false
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;
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;
Types in C#
C# supports two kinds of types: value types and reference types.
Types Description
Value Types Includes simple data types such as int, char, bool, enums
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;
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.

http://www.c-sharpcorner.com/Language/cs_lang_2.asp (1 of 2) [11/27/2001 7:20:02 PM]
C# Corner: C# and .NET Developer's Network
About Us
Books
Book Chapters
Career Center
Discussion
Downloads
Events
Hosting
Links
Magazines
Members
News Letter
Sponsors
Training
Tools
Jobs
Join C# Corner
Our Partners
Submit Code
Win Prizes
Mindcracker
MyClass cls1 = new MyClass();
Data Type Conversions
C# supports two types of conversions. Implicit conversions and explicit conversions.
Implicit conversions are direct conversion. For example:
int iVal = 34;
long lVal = intValue;

Explicit conversions includes type casting. conversion. For example:
long lVal = 123456;
int iVal = (int) lVal;




Mahesh is Admin and the founder of C# Corner. He has been programming in C++, MFC, Visual Basic, COM, ATL, Database
Programming over 5 years.
More articles by Mahesh

contact: webmaster@c-sharpcorner.com
copyright © 2000 c-sharpcorner.com. All rights are reserved. See
terms and condition to use this site and its contents.
Sponsors:
devexpress microgold apress

http://www.c-sharpcorner.com/Language/cs_lang_2.asp (2 of 2) [11/27/2001 7:20:02 PM]
C# Corner: C# and .NET Developer's Network




All Source Code
ADO.NET
Exception
Handling
General
GDI+
Internet
Mobiling
Multithreading
Networking
Printing
Security
Strings & Arrays
Windows Forms
WebForms
XML.NET
-
Articles
Effective C#
FAQ
How do I
Learn C#
Tutorials
Source Code
Articles
FAQ
Learn VB.NET
Tutorials
Source Code
Articles
FAQ
Learn ASP.NET
Tutorials
COBOL.NET
Eiffel.NET
FoxPro.NET
JScript .NET
Learn XML
Mobile & SOAP
My Services
Open Source
VBScript .NET
VC++.NET
Visual J#
Visual Studio.NET
Control Statements
Author: Amisha Mehta
Control Statements:
This lesson shows how to use c# control statements, and the difference between these controls in c++/ JAVA and c#.
The first selection statement is programmer’s favorite “if” statement. It has three forms:
1. Single selection
2. if-then-else selection
3.
multi-case selection
List: 1 IfTest.cs
//understanding if statement

using System;

class IfTest {

public static void Main(){

string s;
int i;

Console.WriteLine("Enter a Number: ");
s = Console.ReadLine();
i = Int32.Parse(s);

//single selection

if(i > 0)
Console.WriteLine("The number {0} is positive",i);

//if-then-else selection


if(i > 0)
Console.WriteLine("The number {0} is positive",i);
else
Console.WriteLine("The number {0} is not positive",i);

//multi-case selection

if(i == 0)
Console.WriteLine("The number is zero");
else if(i > 0)
Console.WriteLine("The number {0} is positive",i);
else
Console.WriteLine("The number {0} is negative",i);

}
}
This program reads a number from console. Input coming from console is in string format. Int32.parse(string) is used to
convert string literal to integer.

http://www.c-sharpcorner.com/Language/ControlStatements1.asp (1 of 6) [11/27/2001 7:20:30 PM]
C# Corner: C# and .NET Developer's Network
About Us
Books
Book Chapters
Career Center
Discussion
Downloads
Events
Hosting
Links
Magazines
Members
News Letter
Sponsors
Training
Tools
Jobs
Join C# Corner
Our Partners
Submit Code
Win Prizes
Mindcracker
The variable i is the object of evaluation here. c++ programmers can see that the use of if statement is same in c#. Halt it.
There is one difference. The expression in an if statement must resolve to bool value. Take a look at following code.
list: 2
using System;

class IfTest2 {

public static void Main(){

if(1)
Console.WriteLine("The if statement executed");
}
}

when this code is complied by c# compiler, it will give the error “constant value 1 can not be converted to bool”.
conditional or ( || ) and conditional and ( && ) operators are used in the same manner. Consider the following code.
List: 3 LeapTest.cs
//Leap year
using System;

class LeapTest {

public static void Main(){

int year;

Console.WriteLine("enter the year value (yyyy) :");
year = Int32.Parse(Console.ReadLine());

if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 )
Console.WriteLine("The year {0} is leap year ",year);
else
Console.WriteLine("The year {0} is not leap year ",year);


}
}

Similar to if-else if-else if-else form of the if statement is the “switch” statement.
List: 4 SwitchTest1.cs
using System;

class SwitchTest1 {

public static void Main(){

int i = 1;

switch(i){
case 1 :
Console.WriteLine("one");
break;
default :
Console.WriteLine("default");
}
http://www.c-sharpcorner.com/Language/ControlStatements1.asp (2 of 6) [11/27/2001 7:20:30 PM]
C# Corner: C# and .NET Developer's Network

}
}

the difference here is if you try to compile above code without putting break statement in case 1, compiler will raise an
error “control can not fall through from one case label”. I’m sure c++/Java programmers are going to love this. Here is
another good news.
The switch expression can evaluate to following types: sbyte, byte,short, ushort, int, uint, long, ulong, char, string or
enum. So the following code is perfectly OK with c# compiler.
List: 5 SwitchTest2.cs
using System;

class SwitchTest2 {
public static void Main(){

string day;

Console.WriteLine("enter the day :");
day = Console.ReadLine();

switch(day){
case "Mon" :
case "Monday" :
Console.WriteLine("day is Monday: go to work");
break;

default :
Console.WriteLine("default");
}

}
}

Control Statements – Loops
This lesson shows you how to use c# looping statements. In c#, there are four iteration constructs rather than three. There
are familiar for, do, while loops and a new one from Visual basic, foreach.
Let us start with while loop.
The Do and While Statements
List: 1 WhileTest.cs
//find out the number of digits in a given number.

using System;

class WhileTest {

public static void Main(){
int i = 123;
int count = 0;
int n = i;

//while loop may execute zero times.
while(i > 0){
++count;
i = i/10;
http://www.c-sharpcorner.com/Language/ControlStatements1.asp (3 of 6) [11/27/2001 7:20:30 PM]
C# Corner: C# and .NET Developer's Network
}

Console.WriteLine("Number {0} contains {1} digits.",n,count);


}
}
The above code shows simple while loop, which finds out the number of digits in a number. for a given number i = 123,
the loop will execute tree times and hence the value of count will be three at the end of the while loop.
The above code has one logical flaw. If the number i is set to 0, the output of the code will be “Number 0 contains 0
digits.” Actually number 0 is of one digit. Since the condition for the while loop i>0 is false from beginning for the value
i=0, the while loop won’t execute any time and count will be zero. here is a solution:

List: 2 DoTest.cs
//find out the number of digits in a given number.
using System;

class DoTest {

public static void Main(){
int i = 0;
int count = 0;
int n = i;

do{
++count;
i = i/10;
}while(i > 0);

Console.WriteLine("Number {0} contains {1} digits.",n,count);


}
}

The Do-While construct checks condition in the end of the loop. Thus Do-while loop will execute atleast once even though
the condition to be checked is false from beginning.
The For Statement
//For loop with break and continue statements

List: 3 Fortest.cs

using System;

class ForTest {

public static void Main(){

for(int i = 0 ; i < 20 ; ++i){
if(i == 10)
break;
if(i == 5)
continue;

Console.WriteLine(i);
}
}
http://www.c-sharpcorner.com/Language/ControlStatements1.asp (4 of 6) [11/27/2001 7:20:30 PM]
C# Corner: C# and .NET Developer's Network
}

The For loop is good when we know how many times the loop needs to be executed. the output of the above code will be:
0
1
2
3
4
6
7
8
9

isn’t it self explanatory? when i becomes 5, the loop will skip over the remaining statements in the loop and go back to the
post loop action. thus 5 is not part of the output. when i becomes 10, control will break out of the loop.
The foreach statement
This statement allows to iterate over the elements in arrays and collections. here is a simple example.

List: 4 ForEach.cs

//foreach loop

using System;

class ForEach {

public static void Main(){

string[] a = {"Chirag","Bhargav","Tejas"};

foreach(string b in a)
Console.WriteLine(b);

}
}

Within the foreach loop parenthesis , the expression is made up of two parts separated by keyword in. The right hand side
of in is the collection and left hand side is the variable with type identifier matching to whatever type the collection returns.

Every time the collection is queried for a new value. As long as the collection returns value, the value is put into the
variable and expression will return true. when the collection is fully traversed, the expression will return false and control
will be transferred to the next statement after a loop.
http://www.c-sharpcorner.com/Language/ControlStatements1.asp (5 of 6) [11/27/2001 7:20:30 PM]
C# Corner: C# and .NET Developer's Network

Enjoy Coding.
About the Author: Amisha Mehta, currently working as Academic Head with STG, Ahmedabad, INDIA. More
details are coming soon

contact: webmaster@c-sharpcorner.com
copyright © 2000 c-sharpcorner.com. All rights are reserved. See
terms and condition to use this site and its contents.
Sponsors:
devexpress microgold apress

http://www.c-sharpcorner.com/Language/ControlStatements1.asp (6 of 6) [11/27/2001 7:20:30 PM]

Xem chi tiết: Tài liệu C# Corner ppt


Không có nhận xét nào:

Đăng nhận xét