Intro  to  .NET  and  Core  C#   Jussi  Pohjolainen  
.NET  Framework   •  So=ware  framework  developed  by  MS   –  Runs  primarily  on  windows   •  Benefits   –  Support  for  numerous  programming  languages   –  Language  integra2on   •  You  can  mix  different  languages   –  Comprehensive  base  class  library   –  Apps  runs  on  top  of  virtual  machine   •  Support  for  other  operaIng  systems  (in  theory)   –  Great  tools:  Visual  Studio  
Common  Language  Infrastructure  (CLI)  
CLI   •  CLI  is  an  open  specificaIon  that  describes   executable  code  and  runIme  environment   •  CLI  is  core  of   –  Microso=  .NET  Framework   –  Mono  (Open  Source)   –  Portable.net  (Open  Source)    
CTS,  CLS,  CIL   •  Common  Type  System  (CTS)   –  A  set  of  data  types  and  operaIons  that  are  share  by   all  CTS-­‐compliant  programming  languages,  such  as  C#   and  VB   •  Common  Language  SpecificaIon  (CLS)   –  Set  of  base  rules  to  which  any  language  targeIng  the   CLI  should  conform.     •  Common  Intermediate  Language  (CIL)   –  Intermediate  language  that  is  abstracted  from  the   plaTorm  hardware  (In  Java:  class)  
Mono:OSX  »  C#  File  
Mono:OSX  »  Building  
Mono:OSX  »  Running  
Developer  Command  Prompt  for  VS2012  
Common  Language  RunIme:  Mac   Dropbox  –   folder  
Common  Language  RunIme:  Win   And  run   the  .exe  in   Windows!  
Compiling  Several  Files  in  C#   C:CodeSample> csc /main:A /out:example.exe A.cs B.cs C:CodeSample> example.exe Hello World from class A C:CodeSample> csc /main:B /out:example.exe A.cs B.cs C:CodeSample> example.exe Hello World from class B
Open  Source  DistribuIons   •  h_p://www.mono-­‐project.com   •  h_p://www.gnu.org/so=ware/dotgnu/ pnet.html  
IDEs   •  You  can  compile  and  run  apps  in  commandline   –  Several  good  text  editors:  notepad++,  sublime   text,  emacs   •  SharpDevelop   –  Open  source  IDE  for  .NET   •  MS  Visual  Studio  Express   –  Object  browsing  tools,  GUI  ediIng,  Intellisense  
Visual  Studio  
C#   •  C#  is  very  similar  to  Java   –  C#,  Java,  ObjecIve-­‐C  and  C++  are  a  member  of  C   family  of  programming  languages   •  C#  provides  more  syntax  than  Java,  although   Java  7  (and  upcoming  8)  is  “catching  up”   •  In  this  course,  we  focus  on  C#  (instead  of   other  CIL  complient  languages)  
About  Namespaces   •  Libraries  give  you  exisIng  code  to  use   •  In  .NET:  namespaces   –  Using  System.CollecIons;   –  Using  System.IO;   –  Using  System.XML;   •  System  is  not  the  root,  you  may  find  namespaces   like  Microso=  that  provides  services  unique  to   Windows  (can’t  run  on  other  systems)   •  Namespaces  are  linked  to  files,  assemblies.  One   assembly  (mscorlib.dll)  can  contain  number  of   namespaces.  
Assemblies:  c:/windows/assembly  
Adding  a  Assembly  in  VS   •  When  wriIng   –  Using  System.Windows.Forms;   •  This  uses  a  assembly   System.Windows.Forms.dll   •  You  must  add  this  as  a  reference  in  your   project!   •  How?   –  In  VS:  SoluIon  Explorer  >  References  >  Add   Reference  
C#   using System; class MyApp { public static void Main() { Calculator c = new Calculator(); Console.Write(c.calculate(5,5)); } } class Calculator { public int calculate(int a, int b) { return a + b; } }
Ildasm.exe:  examine  the  CIL  
About  Windows  8   •  Visual  Studio  2012  provides  templates  for  Win   8  app  development   •  EnIrely  new  runIme:  Windows  Run2me   (WinRT)   •  New  namespaces,  starIng  with  Windows •  App  development  very  similar  to  building   a  .NET  app:  can  be  constructed  using  C#,   visual  basic,  JS  or  C++   •  Heavy  usage  of  XML-­‐based  grammar  XAML
.NET  Role  under  Win8   •  In  addiIon  of  using  Windows.*  namespaces   you  can  use  large  subset  of  .NET  plaAorm   •  We  are  not  focusing  on  this,  but  exclusively   on  .NET  
VISUAL  STUDIO  TIPS  
Visual  Studio  Tips   •  Refer  to  external  assemblies?   –  VIEW  >  SoluIon  Explorer   •  UIliIty  for  invesIgaIng  assemblies   –  VIEW  >  Object  Browser  UIlity   •  Project  properIes?   –  VIEW  >  SoluIon  Explorer  >  ProperIes   •  Class  Tree  View?   –  VIEW  >  Class  View  
Code  Refactoring  and  Snippets   •  Right  click  on  code  and  choose  refactor   –  Extract  method   –  Encapsulate  Field   –  Extract  Interface   –  …   •  When  wriIng  code,  VS  make  suggesIons.   When  finding  the  right  one,  press  tab  twice.   –  Press  esc  to  exit  
DocumentaIon   •  .NET  Framework  4.5  documentaIon  available   online:   –  h_p://msdn.microso=.com/library   –  Use  the  tree  hierarchy  leE  to  navigate  
Almost  the  Same  but  Not  Quite   C#  
Keywords   •  Single  rooted  class  hierarchy:  all  objects   inheritate  System.Object   •  Almost  every  keyword  in  Java  can  be  found   from  C#   –  super -> base –  instanceof -> is –  import -> using –  extends / implements -> : •  Otherwise,  pre_y  much  the  same  
Memory  Handling  and  RunIme   •  Memory  Handling   –  Most  objects  in  C#  to  heap  using  new   –  CLR  handles  garbage  collecIons   •  RunIme   –  C#  is  compiled  to  intermediate  langage  (IL)   –  IL  runs  on  top  of  CLR   –  IL  code  is  always  naIvely  compiled  before  running  
using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello"); Console.ReadLine(); } }
using System; class Program { static int Main(string[] args) { Console.WriteLine("Hello"); Console.ReadLine(); return -1; } }
using System; class Program { static int Main() { Console.WriteLine("Hello"); Console.ReadLine(); } }
System.Console   •  WriteLine(),  ReadLine()   •  Beep()   •  BackgroundColor   •  ForegroundColor   •  Title   •  WindowHeight  /  Width  /  Top  /  Le=   •  Clear  
Formatng  Console  Output   •  Supports  prinT()  statement  of  C.   –  Console.WriteLine(“Hello  {0}!  You  are  {1}  years  old”,   userName,  userAge);   •  Can  use  formatng   –  //  000099999   –  Console.WriteLine(“{0:d9}”,  99999);   –  c,  format  currency   –  d,  format  decimal  numbers   –  f,  format  fixed  point     •  Can  be  used  also  in  Strings:   –  string  message  =  string.Format(“{0:d9}”,  99999);  
Data  Types   •  Datatypes  are  “snippets”  for  full  blown  types   in  namespaces!   •  bool  =>  System.Boolean   •  byte  =>  System.Byte   •  short,  int,  long,  char,  float,  double,  decimal   (128  bit),  string,  Object   •  So  this  works!   –  Console.Write(12.toString());  
Members  of  Datatypes   •  int.MaxValue   •  int.MinValue   •  double.maxValue   •  double.minValue   •  char.isDigit(myChar)   •  char.isLe_er(myChar)   •  bool  b  =  bool.Parse(“true”);   •  double  d  =  double.Parse(“99.9”);  
String   •  Length   •  Compare()   •  Equals()   •  Format()   •  Insert()   •  Remove()   •  Replace()   •  Trim()…  
VerbaIm  Strings   String longstring = @”this is a very long string as you can see”; Console.WriteLine(longstring);
Comparing   •  Usually  we  compare  memory  addresses   –  object1  ==  object2   •  When  using  strings,  this  is  not  the  case.  You   can  compare  strings  like  this  (unlike  in  Java)   –  string1  ==  string2  
Immutable   •  Strings  are  immutable!  Once  assigned  it   cannot  be  changed..   –  String  newString  =  original.toUpper();   •  Even  in  here   –  string  s1  =  “a”;   –  s1  =  “b”;   •  A  new  string  is  created!   •  When  use  of  heavy  textual  data,  don’t  use   strings.    Use  StringBuilder.  
Implicitly  typed  variables   •  Usually  we  do  this   –  int  a  =  5;   –  bool  b  =  true;   •  But  we  can  do  also   –  var  a  =  5;   –  var  b  =  true;   •  Why?  Think  of  this:   –  var  c  =  SomeMethod();  
C#  For  each   string [] array = {“a”, “b”, “c”}; foreach(string c in array) { Console.WriteLine(c); }

Intro to .NET and Core C#

  • 1.
    Intro  to  .NET  and  Core  C#   Jussi  Pohjolainen  
  • 2.
    .NET  Framework   • So=ware  framework  developed  by  MS   –  Runs  primarily  on  windows   •  Benefits   –  Support  for  numerous  programming  languages   –  Language  integra2on   •  You  can  mix  different  languages   –  Comprehensive  base  class  library   –  Apps  runs  on  top  of  virtual  machine   •  Support  for  other  operaIng  systems  (in  theory)   –  Great  tools:  Visual  Studio  
  • 4.
  • 5.
    CLI   •  CLI  is  an  open  specificaIon  that  describes   executable  code  and  runIme  environment   •  CLI  is  core  of   –  Microso=  .NET  Framework   –  Mono  (Open  Source)   –  Portable.net  (Open  Source)    
  • 6.
    CTS,  CLS,  CIL   •  Common  Type  System  (CTS)   –  A  set  of  data  types  and  operaIons  that  are  share  by   all  CTS-­‐compliant  programming  languages,  such  as  C#   and  VB   •  Common  Language  SpecificaIon  (CLS)   –  Set  of  base  rules  to  which  any  language  targeIng  the   CLI  should  conform.     •  Common  Intermediate  Language  (CIL)   –  Intermediate  language  that  is  abstracted  from  the   plaTorm  hardware  (In  Java:  class)  
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
    Common  Language  RunIme:  Mac   Dropbox  –   folder  
  • 12.
    Common  Language  RunIme:  Win   And  run   the  .exe  in   Windows!  
  • 13.
    Compiling  Several  Files  in  C#   C:CodeSample> csc /main:A /out:example.exe A.cs B.cs C:CodeSample> example.exe Hello World from class A C:CodeSample> csc /main:B /out:example.exe A.cs B.cs C:CodeSample> example.exe Hello World from class B
  • 14.
    Open  Source  DistribuIons   •  h_p://www.mono-­‐project.com   •  h_p://www.gnu.org/so=ware/dotgnu/ pnet.html  
  • 15.
    IDEs   •  You  can  compile  and  run  apps  in  commandline   –  Several  good  text  editors:  notepad++,  sublime   text,  emacs   •  SharpDevelop   –  Open  source  IDE  for  .NET   •  MS  Visual  Studio  Express   –  Object  browsing  tools,  GUI  ediIng,  Intellisense  
  • 16.
  • 17.
    C#   •  C#  is  very  similar  to  Java   –  C#,  Java,  ObjecIve-­‐C  and  C++  are  a  member  of  C   family  of  programming  languages   •  C#  provides  more  syntax  than  Java,  although   Java  7  (and  upcoming  8)  is  “catching  up”   •  In  this  course,  we  focus  on  C#  (instead  of   other  CIL  complient  languages)  
  • 18.
    About  Namespaces   • Libraries  give  you  exisIng  code  to  use   •  In  .NET:  namespaces   –  Using  System.CollecIons;   –  Using  System.IO;   –  Using  System.XML;   •  System  is  not  the  root,  you  may  find  namespaces   like  Microso=  that  provides  services  unique  to   Windows  (can’t  run  on  other  systems)   •  Namespaces  are  linked  to  files,  assemblies.  One   assembly  (mscorlib.dll)  can  contain  number  of   namespaces.  
  • 19.
  • 20.
    Adding  a  Assembly  in  VS   •  When  wriIng   –  Using  System.Windows.Forms;   •  This  uses  a  assembly   System.Windows.Forms.dll   •  You  must  add  this  as  a  reference  in  your   project!   •  How?   –  In  VS:  SoluIon  Explorer  >  References  >  Add   Reference  
  • 21.
    C#   using System; classMyApp { public static void Main() { Calculator c = new Calculator(); Console.Write(c.calculate(5,5)); } } class Calculator { public int calculate(int a, int b) { return a + b; } }
  • 22.
  • 23.
    About  Windows  8   •  Visual  Studio  2012  provides  templates  for  Win   8  app  development   •  EnIrely  new  runIme:  Windows  Run2me   (WinRT)   •  New  namespaces,  starIng  with  Windows •  App  development  very  similar  to  building   a  .NET  app:  can  be  constructed  using  C#,   visual  basic,  JS  or  C++   •  Heavy  usage  of  XML-­‐based  grammar  XAML
  • 24.
    .NET  Role  under  Win8   •  In  addiIon  of  using  Windows.*  namespaces   you  can  use  large  subset  of  .NET  plaAorm   •  We  are  not  focusing  on  this,  but  exclusively   on  .NET  
  • 25.
  • 26.
    Visual  Studio  Tips   •  Refer  to  external  assemblies?   –  VIEW  >  SoluIon  Explorer   •  UIliIty  for  invesIgaIng  assemblies   –  VIEW  >  Object  Browser  UIlity   •  Project  properIes?   –  VIEW  >  SoluIon  Explorer  >  ProperIes   •  Class  Tree  View?   –  VIEW  >  Class  View  
  • 27.
    Code  Refactoring  and  Snippets   •  Right  click  on  code  and  choose  refactor   –  Extract  method   –  Encapsulate  Field   –  Extract  Interface   –  …   •  When  wriIng  code,  VS  make  suggesIons.   When  finding  the  right  one,  press  tab  twice.   –  Press  esc  to  exit  
  • 28.
    DocumentaIon   •  .NET  Framework  4.5  documentaIon  available   online:   –  h_p://msdn.microso=.com/library   –  Use  the  tree  hierarchy  leE  to  navigate  
  • 29.
    Almost  the  Same  but  Not  Quite   C#  
  • 30.
    Keywords   •  Single  rooted  class  hierarchy:  all  objects   inheritate  System.Object   •  Almost  every  keyword  in  Java  can  be  found   from  C#   –  super -> base –  instanceof -> is –  import -> using –  extends / implements -> : •  Otherwise,  pre_y  much  the  same  
  • 31.
    Memory  Handling  and  RunIme   •  Memory  Handling   –  Most  objects  in  C#  to  heap  using  new   –  CLR  handles  garbage  collecIons   •  RunIme   –  C#  is  compiled  to  intermediate  langage  (IL)   –  IL  runs  on  top  of  CLR   –  IL  code  is  always  naIvely  compiled  before  running  
  • 32.
    using System; class Program { static void Main(string[] args) { Console.WriteLine("Hello"); Console.ReadLine(); } }
  • 33.
    using System; class Program { static int Main(string[] args) { Console.WriteLine("Hello"); Console.ReadLine(); return -1; } }
  • 34.
    using System; class Program { static int Main() { Console.WriteLine("Hello"); Console.ReadLine(); } }
  • 35.
    System.Console   •  WriteLine(),  ReadLine()   •  Beep()   •  BackgroundColor   •  ForegroundColor   •  Title   •  WindowHeight  /  Width  /  Top  /  Le=   •  Clear  
  • 36.
    Formatng  Console  Output   •  Supports  prinT()  statement  of  C.   –  Console.WriteLine(“Hello  {0}!  You  are  {1}  years  old”,   userName,  userAge);   •  Can  use  formatng   –  //  000099999   –  Console.WriteLine(“{0:d9}”,  99999);   –  c,  format  currency   –  d,  format  decimal  numbers   –  f,  format  fixed  point     •  Can  be  used  also  in  Strings:   –  string  message  =  string.Format(“{0:d9}”,  99999);  
  • 37.
    Data  Types   • Datatypes  are  “snippets”  for  full  blown  types   in  namespaces!   •  bool  =>  System.Boolean   •  byte  =>  System.Byte   •  short,  int,  long,  char,  float,  double,  decimal   (128  bit),  string,  Object   •  So  this  works!   –  Console.Write(12.toString());  
  • 38.
    Members  of  Datatypes   •  int.MaxValue   •  int.MinValue   •  double.maxValue   •  double.minValue   •  char.isDigit(myChar)   •  char.isLe_er(myChar)   •  bool  b  =  bool.Parse(“true”);   •  double  d  =  double.Parse(“99.9”);  
  • 39.
    String   •  Length   •  Compare()   •  Equals()   •  Format()   •  Insert()   •  Remove()   •  Replace()   •  Trim()…  
  • 40.
    VerbaIm  Strings   Stringlongstring = @”this is a very long string as you can see”; Console.WriteLine(longstring);
  • 41.
    Comparing   •  Usually  we  compare  memory  addresses   –  object1  ==  object2   •  When  using  strings,  this  is  not  the  case.  You   can  compare  strings  like  this  (unlike  in  Java)   –  string1  ==  string2  
  • 42.
    Immutable   •  Strings  are  immutable!  Once  assigned  it   cannot  be  changed..   –  String  newString  =  original.toUpper();   •  Even  in  here   –  string  s1  =  “a”;   –  s1  =  “b”;   •  A  new  string  is  created!   •  When  use  of  heavy  textual  data,  don’t  use   strings.    Use  StringBuilder.  
  • 43.
    Implicitly  typed  variables   •  Usually  we  do  this   –  int  a  =  5;   –  bool  b  =  true;   •  But  we  can  do  also   –  var  a  =  5;   –  var  b  =  true;   •  Why?  Think  of  this:   –  var  c  =  SomeMethod();  
  • 44.
    C#  For  each   string [] array = {“a”, “b”, “c”}; foreach(string c in array) { Console.WriteLine(c); }