Upload
peregrine-morton
View
220
Download
0
Tags:
Embed Size (px)
Citation preview
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
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();
}
}
}
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
Debugging, Disable Squigglylines
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)
Command Line compile and execution
• C:\Windows\Microsoft.NET\Framework64\v4.0.30319• csc.exe• csc Program.cs
Comments, IntelliSense
// . . .
/* . . . */
Use Line numbers (Tools->Options)
Data Types, variables
x = 2
y = x + 1;
Console.WriteLine(y)
…
3
Need Variables
Data Types, variables (cont)
C# Variables Declaration and Naming
Primitives (though they are Objects)http://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx
object
String
Nullable Type (a bit advance)
King System.Object
char
stringint
Primitives are Objects
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");
}
}
}
All are objects!!!
small integers (singed and unsinged)
big integers (singed and unsinged)
float and double, decimal
Special Floating numbers
(Division by 0)
Doubles vs Decimals
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
Characters and Strings
Strings methods* (again later)
Immutable String * (again later)
StringBuilder* (again later)
Variable Scope *(again later)
MyMethod
Type Conversion (cast)
Back to our example: using integer data type
int x = 2;
int y = x + 1;
Console.WriteLine(y);
Case sensitive language
string myName; myName = "Andrew"; Console.WriteLine(myName);
string myname = "Alice"; Console.WriteLine(myname);
one line
Introducing var
• Don’t be afraid
var myName1 = "Bill";
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;