• Save
Intro to .NET and Core C#
Upcoming SlideShare
Loading in...5
×
 

Intro to .NET and Core C#

on

  • 1,311 views

 

Statistics

Views

Total Views
1,311
Views on SlideShare
1,311
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Intro to .NET and Core C# Intro to .NET and Core C# Presentation Transcript

  • 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.csC:CodeSample> example.exe Hello World from class AC:CodeSample> csc /main:B /out:example.exe A.cs B.csC: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);}