51
An introduction to C# SynapseIndia Reviews on DOTNET Development Fundamentals

SynapseIndia Reviews on DOTNET Development Fundamentals

Embed Size (px)

DESCRIPTION

SynapseIndia Reviews on DOTNET Development FundamentalsSynapseIndia Employee Complaints,SynapseIndia Employee Reviews,SynapseIndia Court Cases,SynapseIndia Employee Feedback,SynapseIndia Employee Bond

Citation preview

Page 1: SynapseIndia Reviews on DOTNET Development Fundamentals

An introduction to C#

SynapseIndia Reviews on DOTNET Development

Fundamentals

Page 2: SynapseIndia Reviews on DOTNET Development Fundamentals

An introduction to C#

2

C# is a language that was developed by Microsoft specifically targeted for the .NET platform.

Page 3: SynapseIndia Reviews on DOTNET Development Fundamentals

C# Features

3

No pointers Automatic memory management Completely object-oriented Supports interface-based programming

Both implementation inheritance and interface inheritance supported

Support for overloaded operators Support for aspect-based (attribute-

based) programming Can only produce managed-code

Page 4: SynapseIndia Reviews on DOTNET Development Fundamentals

Let’s try a simple program.

4

Page 5: SynapseIndia Reviews on DOTNET Development Fundamentals

5

Create a Working Directory

mkdir k:\week1\hello Copy corvars.bat

copy c:\Program Files\Microsoft Visual Studio.NET \FrameworkSDK\bin\corvars.bat

k:\week1\hello\corvars.bat Set up environment, run covars.bat

Cd k:\week1\helloType corvars.bat <hit return>

Page 6: SynapseIndia Reviews on DOTNET Development Fundamentals

Open notepad, create the following file and save it in the k:\week1\hello directory as hello.cs.

6

class Hello

{

static void Main()

{

System.Console.WriteLine("Hello World");

}

}

Page 7: SynapseIndia Reviews on DOTNET Development Fundamentals

7

Compile the programk:\week1\hello>csc Hello.cs

Run the programK:\week1\hello>Hello.exeK:\week1\hello> Hello World

Page 8: SynapseIndia Reviews on DOTNET Development Fundamentals

8

Page 9: SynapseIndia Reviews on DOTNET Development Fundamentals

C#.NET Language Basics

9

•Types in C#

•Defining integer types

•A Bit About Strings

•Reading From and Writing To The Console

•If Then Statement

•Looping – The For Next Statement 

Page 10: SynapseIndia Reviews on DOTNET Development Fundamentals

Primitive Types

10

C# Type .NET Framework type

bool System.Boolean

byte System.Byte

sbyte System.Sbyte

char System.Char

decimal System.Decimal

double System.Double

float System.Single

Page 11: SynapseIndia Reviews on DOTNET Development Fundamentals

Primitive Types (contd.)

11

int System.Int32

uint System.UInt32

long System.Int64

ulong System.UInt64

object System.Object

short System.Int16

ushort System.UInt16

string System.String

Page 12: SynapseIndia Reviews on DOTNET Development Fundamentals

A word on types

12

All types in .NET derive from System.Object They are provided implementations of

ToString() and GetType() To get a string with the type of any variable,

you can call <var>.GetType() Whenever you call Console.WriteLine(obj)

the ToString() method on obj is implicitly called. The default ToString implementation for classes simply returns the name of the class.

Page 13: SynapseIndia Reviews on DOTNET Development Fundamentals

What Are Integers

13

0, 432, -5, 10000000, -10000000

Integers are whole numbers

Integer variables are stored as signed 32-bit (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647.

Page 14: SynapseIndia Reviews on DOTNET Development Fundamentals

Defining Integers

14

int i;

int i, j, k;

int i = 12;

j = i; j is now equal to 12

i = 15;

k = i + j; k is equal to 27

To write an Integer, convert it to a String using:

k.ToString();

Page 15: SynapseIndia Reviews on DOTNET Development Fundamentals

A Bit About Strings

15

Page 16: SynapseIndia Reviews on DOTNET Development Fundamentals

What are strings?

16

“abcdef” “Abcdef” “aBcdEf” “A23+-/*789” “q”

“John J. Smith”

“How do you do?”

“123 South Street, Calais, ME 04235”

“ Are we there? ”

“” an empty string

Page 17: SynapseIndia Reviews on DOTNET Development Fundamentals

How do we define strings?

17

string strTmp;

strTmp = “time will tell”;

string strTmp = “time will tell”;

strTmp = Console.ReadLine();

string strTmp2;

strTmp2 = strTmp;

strTmp2 “time will tell”

Page 18: SynapseIndia Reviews on DOTNET Development Fundamentals

Concatenating Strings

18

string strCity = “Calais”;

string strState = “ME”;

string strZip = “04270”;

string strLoc;

strLoc = strCity + “, ” + strState + “ ” + strZip;

strLoc “Calais, ME 04270”

Page 19: SynapseIndia Reviews on DOTNET Development Fundamentals

Some String Functions

19

string strTmp;

strTmp.Trim(); – removes leading and trailing spaces

strTmp.ToUpper(); – converts string to all upper case

strTmp.ToLower(); – converts string to all lower case

strTmp.Length; – returns string length as an integer

strTmp.SubString() – extracts a substring

Page 20: SynapseIndia Reviews on DOTNET Development Fundamentals

String Function Examples

20

string strTmp = “ Hello World ”;

strTmp.Trim();

strTmp “Hello World”

string strTmp = “Hello World”;

strTmp.ToLower(); “hello world”

strTmp.ToUpper(); “HELLO WORLD”

Page 21: SynapseIndia Reviews on DOTNET Development Fundamentals

String.Length Function

21

string strTmp;

strTmp = “in the beginning”;

The value of strTmp.Length is 16.

int i;

i = strTmp.Length;

The value of i is 16.

Page 22: SynapseIndia Reviews on DOTNET Development Fundamentals

String.SubString() Function

22

String.Substring(startIndex , length );

Parameters (are Integers)

startIndex – Where the substring starts. startIndex is zero-based.

length – The number of characters in the substring.

Page 23: SynapseIndia Reviews on DOTNET Development Fundamentals

Substring Examples

23

string strTmp;

strTmp = “around the world”;

strTmp.Substring(0,6); “around”

strTmp.Substring(11,5); “world”

strTmp.Substring(0,strTmp.Length);

“around the world”

Page 24: SynapseIndia Reviews on DOTNET Development Fundamentals

Writing to the Console

24

Console.WriteLine(String); write with line return

Console.WriteLine(“Hi There”)

C:\>Hi There

C:\>

Console.Write(String); – write with no line return Console.Write(“Hi There”)

C:\>Hi There

Page 25: SynapseIndia Reviews on DOTNET Development Fundamentals

Reading from the Console

25

Console.ReadLine(); – returns a string

string tmp;

Console.Write(“What is your name? ”);

tmp = Console.ReadLine();

Console.WriteLine(“Hi “ + tmp);

C:\>What is your name? Chip

C:\>Hi Chip

C:\>

Page 26: SynapseIndia Reviews on DOTNET Development Fundamentals

if Statement

26

if (some condition is true)

{

do something in here,

using one or more lines of code…

}

Page 27: SynapseIndia Reviews on DOTNET Development Fundamentals

What is difference between = and ==?

27

= is for assignment of valueString tmpString = “Hello world”;int i = 12;

== is for equivalenceif (str1 == str2) { some code }

if (str.Length == 0) { some code }

if (str1 != “end”) { some code }

Page 28: SynapseIndia Reviews on DOTNET Development Fundamentals

Sample if Statement

28

string strInput ;

strInput = Console.ReadLine();

if (strInput == “”)

{

Console.WriteLine(“Input required.”);

}

Page 29: SynapseIndia Reviews on DOTNET Development Fundamentals

The For Loop

29

Page 30: SynapseIndia Reviews on DOTNET Development Fundamentals

A Simple For Loop

30

int i;

for (i = 1; i<10; i++)

{

Console.WriteLine("The value of i is " + i.ToString());

}

The Value of i is 1

The Value of i is 2

The Value of i is 3

The Value of i is 9

The Value of i is 10

Page 31: SynapseIndia Reviews on DOTNET Development Fundamentals

Or You Could Reverse It…

31

int i;

for (i = 10; i>0; i--)

{

Console.WriteLine("The value of i is " + i.ToString());

}

The Value of i is 10

The Value of i is 9

The Value of i is 8

The Value of i is 2

The Value of i is 1

Page 32: SynapseIndia Reviews on DOTNET Development Fundamentals

To Walk Through a String

32

string tmp = “hello world”;

for (int k = 0; k< tmp.Length-1;k++)

{

Console.WriteLine(tmp.Substring(k,1));

}

Page 33: SynapseIndia Reviews on DOTNET Development Fundamentals

To Walk Through a String Backward

33

string tmp = "hello world";

for (int k =tmp.Length-1;k>-1;k--)

{

Console.WriteLine(tmp.Substring(k,1)); }

Page 34: SynapseIndia Reviews on DOTNET Development Fundamentals

What About?

34

What If We Want To Enter More Data?

What If No String Is Entered?

What If The Entered String Is Too Long?

How Do We Know When We Are Done?

Page 35: SynapseIndia Reviews on DOTNET Development Fundamentals

What If We Want To Enter More Data?

35

Labels

A Label is Defined with a Colon ReturnHere:

goto Statements

goto Statements Direct Program Flow To A Label

goto ReturnHere;

GoTo Statements are Evil and High Risk!!!

Page 36: SynapseIndia Reviews on DOTNET Development Fundamentals

What If No String Is Entered?

36

Checking for a zero length string.

if (tmpStr.Length == 0)

{

Console.WriteLine(“No String Entered”);

goto ReturnHere;

}

Note: You could also check for tmpStr == “”

Page 37: SynapseIndia Reviews on DOTNET Development Fundamentals

What If The Entered String Is Too Long?

37

Let’s only work with strings up to 10 characters…

if (strTmp.Length > 10)

{

strTmp = strTmp.SubString(0,10);

}

Page 38: SynapseIndia Reviews on DOTNET Development Fundamentals

How Do We Know When We Are Done?

38

Let’s check for the string “end” to end the program…

if (strTmp == “end”)

{

return;

}

Note: return tells the program to exit the subroutine, which in this case will end the program.

Page 39: SynapseIndia Reviews on DOTNET Development Fundamentals

Comments in C#

39

Both /* … */ and // can be used for comments. VS provides comment/uncomment selections.

Use the menu bar, or Ctrl-K Ctrl-C for comment and Ctrl-K Ctrl-U for uncomment

Page 40: SynapseIndia Reviews on DOTNET Development Fundamentals

40

Page 41: SynapseIndia Reviews on DOTNET Development Fundamentals

Now let’s redo hello.cs as a Visual Studio project.

41

Page 42: SynapseIndia Reviews on DOTNET Development Fundamentals

Visual Studio.NET

42

The newest version of Visual Studio Multiple language development finally

in one environment. Can program in

Visual C# Visual Basic.NET Visual C++.NET

Can build Desktop console and GUI applications Web services ASP.NET Web applications Mobile applications

Page 43: SynapseIndia Reviews on DOTNET Development Fundamentals

To get Visual Studio.NET

43

You need to purchase either: An MSDN subscription A copy of Visual Studio.NET

Academic editions are available (in or through the bookstore ?)

Page 44: SynapseIndia Reviews on DOTNET Development Fundamentals

Visual Studio.NET

44

Start up Visual Studio.NET Open a new project by either:

Clicking on the New Project button on the Start Page OR

File-> New-> Project from the Menu Bar In Visual C# Projects, create a Console

Application Implement the Main() method

Notice you now have IntelliSense Add the Console.WriteLine… line of

code. Compile using the Build menu. Run using the Debug menu

Page 45: SynapseIndia Reviews on DOTNET Development Fundamentals

Your code should look like this…

45

using System;

namespace HelloVS{

/// <summary>/// Summary description for App./// </summary>class App{

/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main(string[] args){

System.Console.WriteLine("Hello World");}

}}

Page 46: SynapseIndia Reviews on DOTNET Development Fundamentals

Class Exercise (time permitting)

46

Using the Visual Studio.NET write an interactive console program to accept information from the keyboard and then format and display the information back. It might be a person’s name and address or a variable list of favorite pets including name and type of animal or whatever. Focus on formatting the data, looping to accept multiple entries, testing for missing information and also testing for an at end condition. A sample, somewhat simplified example is in the Class Collections zip file on the web (i.e. www.PondviewSoftware.com).

Page 47: SynapseIndia Reviews on DOTNET Development Fundamentals

47

Page 48: SynapseIndia Reviews on DOTNET Development Fundamentals

Homework Part 1

48

Send Me An Email [email protected], include the following: 

1. Full Name, Nick Name, Student ID #

2. Home Phone / Work Phone

3. Email Address (s) [email gives me one]

4. Your background in computing/programming

5. Any experience with .NET, other courses, ?

6. Your objective or goals for this course

7. Any material you have a special interest in covering?

8. Any issues or questions that you have?

9. Which additional dates you would attend classes on.

Page 49: SynapseIndia Reviews on DOTNET Development Fundamentals

Homework Part 2

49

C#.Net Programming

Homework Assignment – Week 1

Assignment Due: November 13, 2003 5:30 PM

 

 

Page 50: SynapseIndia Reviews on DOTNET Development Fundamentals

Write a C#.NET Console Application, which performs the following:

50

1. Accepts a first name string, a middle name string, and a last name string from the console. The first name and last name are required. The middle name is optional.

2. Concatenates the two or three fields together creating a full name string.

3. Truncates the full name to 20 characters if the length of the full name is longer than 20 characters.

4. Provides the capability of displaying the full name either vertically or horizontally and forward or backward as desired.

5. Allows the full name field to be displayed in either of these four ways as many times as desired.

6. Allows the user to go back to the top and start over, entering a new name.

7. Terminates gracefully. Note:  There was enough information discussed in today’s class to

complete this assignment. Feel free to use any additional commands, structures, or functions you wish.

Page 51: SynapseIndia Reviews on DOTNET Development Fundamentals

51