Introduction to the C# Programming Language for the VB Programmer

51
Introduction to the C# Programming Language for the VB Programmer

description

Introduction to the C# Programming Language for the VB Programmer. Lecture Overview. Some key terms Introduce the C# programming language for the VB developer Mention some important features of the Visual Studio .NET environment Pass along some editing tips and tricks. Key Terms (1). - PowerPoint PPT Presentation

Transcript of Introduction to the C# Programming Language for the VB Programmer

Page 1: Introduction to the  C# Programming Language for the VB Programmer

Introduction to the C# Programming

Language for the VB Programmer

Page 2: Introduction to the  C# Programming Language for the VB Programmer

Slide 2

Lecture Overview Some key terms Introduce the C# programming

language for the VB developer Mention some important features of the

Visual Studio .NET environment Pass along some editing tips and tricks

Page 3: Introduction to the  C# Programming Language for the VB Programmer

Slide 3

Key Terms (1) Constructor – A special method that is called

when an object is created from its class In C# a method having the same name as it’s

class Class – A template from which objects are

created Inheritance – The concept that one class can

be created based on another class The TextBox class, for example

Method – An action that the object can perform

The SetFocus method of a TextBox, for example Property – The data stored in object.

The Text property of a text box, for example

Page 4: Introduction to the  C# Programming Language for the VB Programmer

Slide 4

Key Terms (2) Object – A class instance. Once an

object has been created from its class, it’s possible to call methods and read and write properties Objects are created when a class’

constructor is called Note that some classes are static and

have static methods

Page 5: Introduction to the  C# Programming Language for the VB Programmer

Slide 5

Project Structure Everything is the same as VB

Solution file, project file, program files

Modules have a file suffix of .cs instead of .vb

Page 6: Introduction to the  C# Programming Language for the VB Programmer

Slide 6

THE BIG DIFFERENCE

C# IS CASE

SENSITIVE

Page 7: Introduction to the  C# Programming Language for the VB Programmer

Slide 7

Statements In C#, all statements end with a semicolon ;

Statements can span multiple lines There is no need for a continuation character

as in VB C# example

System.Console.WriteLine( “This is a line of text”);

VB exampleSystem.Console.WriteLine( _ “This is a line of text”);

Page 8: Introduction to the  C# Programming Language for the VB Programmer

Slide 8

Comments XMLSummary comments begin with /// Single line comments are marked

with // Multi-line comments start with /* and

end with */

// A comment.

/* This is Also a comment */

Page 9: Introduction to the  C# Programming Language for the VB Programmer

Slide 9

Variable Declarations Variable declarations work the same

way in both C# and VB Local variables are declared inside of a

procedure Module-level variables are declared

outside of a procedure In C#, the declaration syntax is

reversed from VB

Page 10: Introduction to the  C# Programming Language for the VB Programmer

Slide 10

Variable Declaration (Examples) VB

Dim Count As IntegerDim MyName As String = “Ekedahl”

C#int count;string MyName = “Ekedahl”;

Page 11: Introduction to the  C# Programming Language for the VB Programmer

Slide 11

Variable Declarations (Examples) Access modifiers work the similarly in

VB and C# public, private, protected

VB syntax to declare a module-level variable Private Counter As Integer

C# syntax to declare the same variable private int Counter;

Page 12: Introduction to the  C# Programming Language for the VB Programmer

Slide 12

Data TypesC# VB

int Integer

bool Boolean

string String

double Double

float Single

date DateTime

Page 13: Introduction to the  C# Programming Language for the VB Programmer

Slide 13

Strings Use the System.String data type as in

VB; The C# string type maps to System.String

the string concatenation operator is + instead of &

The members are the same between C# and VB All this because these types are .net

Framework types

Page 14: Introduction to the  C# Programming Language for the VB Programmer

Slide 14

Type Conversion (Introduction) In IS 350, you used val to convert

strings to numbers In C#, val is used to declare implicitly

typed variables We will use a much different strategy

here Each primary data type supports a

method named TryParse The method accepts 2 arguments

The string to parse The output result

Page 15: Introduction to the  C# Programming Language for the VB Programmer

Slide 15

TryParse (Example) Try to parse the string arg and store the

result in outstring arg = "123";double result;if (System.Double.TryParse(arg, out result) == true)

{ return true;}return false;

Page 16: Introduction to the  C# Programming Language for the VB Programmer

Slide 16

Using System.Convert Members of System.Convert class also

convert one type to another System.Convert.ToInt32() System.Convert.ToDouble() System.Convert.ToDateTime() …

Page 17: Introduction to the  C# Programming Language for the VB Programmer

Slide 17

Constants Constants are just variables whose

value never changes Declare with the const statement Constants must be initialized when they

are declared

Page 18: Introduction to the  C# Programming Language for the VB Programmer

Slide 18

Constants (Example) Declare and initialize constants

const int x = 0;public const double gravitationalConstant = 6.673e-11;

private const string productName = "Visual C#";

Page 19: Introduction to the  C# Programming Language for the VB Programmer

Slide 19

Member Scope Access modifiers control the scope of a

procedure or variable The keywords are the same between VB and

C# private – scope is the class containing the

procedure or variable protected – scope is the current class and

derived class public – scope is global – exposed to other

assemblies

Page 20: Introduction to the  C# Programming Language for the VB Programmer

Slide 20

C# Blocks In VB, blocks are marked with keywords

Sub – End Sub If – End If Do – Loop While – End While

In C#, blocks are all marked with {} as in Java or C++

Page 21: Introduction to the  C# Programming Language for the VB Programmer

Slide 21

C# Blocks (Example)namespace Validate{ public static class ValidateNumbers { public static bool IsInteger( string arg) { } }}

Page 22: Introduction to the  C# Programming Language for the VB Programmer

Slide 22

Procedures Visual Basic has Function and Sub

procedures C# works a bit differently

Procedures that don’t return a value have a data type of void

Procedures that do return a value have an explicitly defined data type

Page 23: Introduction to the  C# Programming Language for the VB Programmer

Slide 23

Procedures (Example 1) The following procedure does not return

a value

private void InitializeLocal(){ // Statements}

Page 24: Introduction to the  C# Programming Language for the VB Programmer

Slide 24

Procedures (Example 2) The following procedure returns a value

having a data type of bool (Boolean)

public static bool IsInteger(string arg, out int result)

{ if (System.Int32.TryParse(arg, out result) == true) { return true; } return false;}

Page 25: Introduction to the  C# Programming Language for the VB Programmer

Slide 25

Calling Procedures To call a procedure, use it’s name If the argument list is empty, the () are

required The empty parens are optional in VB

Call the procedure foo without arguments

foo();

Page 26: Introduction to the  C# Programming Language for the VB Programmer

Slide 26

Operators (Arithmetic) Mathematical operators are the same

for both VB and C# with a few exceptions % is the modulus operator (Mod) ++ and -- are post and pre increment and

decrement operators

Increment Count (Example)Count++;

Page 27: Introduction to the  C# Programming Language for the VB Programmer

Slide 27

Operators (Logical) They are pretty much the same from VB

and C# Inequality (<>) in VB is (!=) in C# Equality (=) in VB is (==) in C#

In C#, the single (=) is always used for assignment statements

Page 28: Introduction to the  C# Programming Language for the VB Programmer

Slide 28

Relational Operators These are quite different

VB C#

And &

AndAlso &&

Or |

OrElse ||

Xor ^

Not !

Page 29: Introduction to the  C# Programming Language for the VB Programmer

Slide 29

Decision-Making Statements (if) if statements take a boolean

expression as an argument Note the parentheses are required

if (i >= 0){ // do something if i is // greater than 0}

Page 30: Introduction to the  C# Programming Language for the VB Programmer

Slide 30

Decision-Making Statements ( 2-way if) Use the else keyword to create a 2-way if statement

if ( i >= 0){ // Do something if i is // greater than 0.}else{ // Do something else.}

Page 31: Introduction to the  C# Programming Language for the VB Programmer

Slide 31

Decision-Making Statements ( multi-way if) Use else if to create multi-way decisions if (i > 0){ // Do something if i is // greater than 0.}else if (i < 0){ // Do something else.}else{

// i must be equal to 0.}

Page 32: Introduction to the  C# Programming Language for the VB Programmer

Slide 32

Decision-Making Statements (switch) C# uses the switch statement instead

of Select Case Both work the same way The break keyword must appear at the

end of each case

Page 33: Introduction to the  C# Programming Language for the VB Programmer

Slide 33

switch statement (Example)

switch (day){

case 0:DayOfWeek = “Sunday”;break;

case 1:DayOfWeek = “Monday”;break;

}

Page 34: Introduction to the  C# Programming Language for the VB Programmer

Slide 34

Loops (Introduction) while is used to create a pre-test loop

(Do While) do is used to create a post-test loop (Do Until)

for loops are used when the iteration count is known in advance

Page 35: Introduction to the  C# Programming Language for the VB Programmer

Slide 35

while Loops While loops take a boolean expression

enclosed in parenthesis The loop executes while the condition is

true {} mark the while block instead of End While

Page 36: Introduction to the  C# Programming Language for the VB Programmer

Slide 36

while Loops (Example)int i;while (i < 10){

System.Console.WriteLine(i);i++;

}

Page 37: Introduction to the  C# Programming Language for the VB Programmer

Slide 37

do Loops Do loops test the condition after the

loop has executed once Example:do { System.Console.WriteLine(x); x++; // Post increment operator} while (x < 5);

Page 38: Introduction to the  C# Programming Language for the VB Programmer

Slide 38

for Loops Like VB, for loops can be used when

the number of loop iterations is known in advance The syntax is quite different though

Page 39: Introduction to the  C# Programming Language for the VB Programmer

Slide 39

for Loops (Syntax)for ( init-expr; cond-expr; loop-expr ){ // statement block} int-expr contains the expression’s

initial value cond-expr contains the condition loop-expr updates the expression

Page 40: Introduction to the  C# Programming Language for the VB Programmer

Slide 40

for Loops (Example) Print the counting numbers from 1 to 10

for (int i = 0; i < 10; i++){

System.Console.WriteLine(i);}

Page 41: Introduction to the  C# Programming Language for the VB Programmer

Slide 41

foreach loops foreach loops are used to enumerate

arrays and collections We will talk about collections more later

When using a foreach loop you need not explicitly increment or decrement the counter

Page 42: Introduction to the  C# Programming Language for the VB Programmer

Slide 42

foreach loops (Example) Declare and enumerate a one-

dimensional array named fibarray

int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 };

foreach (int i in fibarray){ System.Console.WriteLine(i);}

Page 43: Introduction to the  C# Programming Language for the VB Programmer

Slide 43

Exiting a Loop Prematurely Use the break statement to exit a loop Use the continue statement to jump to

the loop’s condition The condition is tested immediately

There is a goto statement to jump to a named label but we will NEVER use it

Page 44: Introduction to the  C# Programming Language for the VB Programmer

Slide 44

Importing and Using Namespaces By default, you must fully qualify class

and method names System.IO.StreamReader for example

In VB, the Imports statement allows unqualified references Imports System.IO

In C#, it’s the using statement using System.IO;

Page 45: Introduction to the  C# Programming Language for the VB Programmer

Slide 45

C# Arrays (Declaring) Arrays work similarly in both C# and VB

Instead of using a () for the subscript, use []

When declaring an array, the square brackets appear after the type

The above array is declared as an array of type int Note that

Page 46: Introduction to the  C# Programming Language for the VB Programmer

Slide 46

C# Arrays (Sizing) Arrays must be given a size before they

can be used The new keyword calls the array

constructor

Page 47: Introduction to the  C# Programming Language for the VB Programmer

Slide 47

C# Arrays (Initializing) Arrays can be initialized when they are

declared When declaring and initializing an array

in the same statement, you can omit the size as the value is inferred from the data

Page 48: Introduction to the  C# Programming Language for the VB Programmer

Slide 48

C# Arrays (Multidimensional) You saw 1-dimensional arrays in IS 350 Arrays can have 2, 3, or more

dimensions Declare a 2-dimensional array

Declare and size a 2-dimensional array

Page 49: Introduction to the  C# Programming Language for the VB Programmer

Slide 49

Accessing Array Members You access an array member via a

subscript Store the value 5 in the 4th array

element

2-dimensional arrays have two subscripts

Page 50: Introduction to the  C# Programming Language for the VB Programmer

Slide 50

C# Arrays (Jagged) C# supports jagged arrays but we will

not use them

Page 51: Introduction to the  C# Programming Language for the VB Programmer

Slide 51

ASP.NET Tables (Introduction) You used the <table> tag in IS 350 to

create static tables The <asp:table> element gives us a

rich interface