35
C# Introduction Part 1

C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Embed Size (px)

Citation preview

Page 1: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

C# Introduction Part 1

Page 2: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Which Visual Studio Should I use?

• Any Express (2012, 2013…) or Community Edition 2013• Any full version

Page 3: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

.Net framework

• Class Libraries …Console.ReadLine• MSIL• CLR

Page 4: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

HelloWord, scope {}, method, Main, white space

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace First

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Hello World!");

Console.ReadLine();

}

}

}

Page 5: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

IDE, Projects, solutions, debug and release mode, …

C:\Users\<yourname>\Documents\Visual Studio 2013\Projects - default

Highly Recommend

Use C:\Projects\MIS118

.sln (open using Notepad -> it is xml)

Look at using .csproj

Page 6: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Debugging, Disable Squigglylines

Page 7: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Compiling

code.cpp

Assembly language

Machine language

.exe

C/C++old languages

C#.NET languages

com

pilin

gcode.cs

IntermediateLanguage (MSIL)

+ metadata

Machine language

.exe

C# compiler

JIT compiler

code.vb

VB.NET compiler

CommonLanguageRuntime

(CLR)

Page 8: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Command Line compile and execution

• C:\Windows\Microsoft.NET\Framework64\v4.0.30319• csc.exe• csc Program.cs

Page 10: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Use Line numbers (Tools->Options)

Page 11: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Data Types, variables

x = 2

y = x + 1;

Console.WriteLine(y)

3

Need Variables

Page 12: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Data Types, variables (cont)

Page 13: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

C# Variables Declaration and Naming

Page 14: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Primitives (though they are Objects)http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx

object

String

Page 15: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Nullable Type (a bit advance)

Page 16: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

King System.Object

char

stringint

Page 17: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Primitives are Objects

Page 18: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

is Operator* (will be later again) static void Main(string[] args)

{

int myInt = 5;

TestIsOperator(myInt);

string myString = "5";

TestIsOperator(myString);

Console.ReadLine();

}

private static void TestIsOperator(Object obj)

{

if (obj is int)

{

int i = (int)obj;

Console.WriteLine("Integer received");

}

else if (obj is string)

{

string i = (string)obj;

Console.WriteLine("String received");

}

}

}

Page 19: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

All are objects!!!

Page 20: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

small integers (singed and unsinged)

Page 21: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

big integers (singed and unsinged)

Page 22: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

float and double, decimal

Page 23: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Special Floating numbers

(Division by 0)

Page 24: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Doubles vs Decimals

Page 25: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Ex: float and decimal* (again later)using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace DoubleVsDecimal{ class Program { static void Main(string[] args) { double a = 0.2f; double b = 1.0f;

Console.WriteLine("{0}", a - b);

decimal c = 0.2m; decimal d = 1.0m;

Console.WriteLine("{0}", c - d);

Console.ReadLine(); } }}output: -0.799999997019768-0.8

25

Page 26: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Characters and Strings

Page 27: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Strings methods* (again later)

Page 28: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Immutable String * (again later)

Page 29: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

StringBuilder* (again later)

Page 30: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Variable Scope *(again later)

MyMethod

Page 31: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Type Conversion (cast)

Page 32: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Back to our example: using integer data type

int x = 2;

int y = x + 1;

Console.WriteLine(y);

Page 33: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Case sensitive language

string myName; myName = "Andrew"; Console.WriteLine(myName);

string myname = "Alice"; Console.WriteLine(myname);

one line

Page 34: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Introducing var

• Don’t be afraid

var myName1 = "Bill";

Page 35: C# Introduction Part 1. Which Visual Studio Should I use? Any Express (2012, 2013…) or Community Edition 2013 Any full version

Conversion again* (again later). C# is a Strongly Typed Language

string s = "Angie"; int times = 100; Console.WriteLine(times + s);

string ok = times + s;

int bad = times + s;