Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to C#

Introduction to C#

Presented and Written by Alexander McLean

Avatar for Society of Software Engineers

Society of Software Engineers

September 17, 2014

Other Decks in Technology

Transcript

  1. What is C#? • General-purpose, type-safe, object-oriented programming language •

    Geared toward programmer productivity • Chief Architect – Anders Hejlsberg – Turbo Pascal – Delphi • Microsoft’s answer to Java • Platform-neutral • Written for Microsoft .NET Framework
  2. Object-Orientation • Unified type system – All types share common

    base type • Classes and interfaces – Interface describes members – Class provides implementation for members • Properties, methods, and events – Function members – Properties encapsulate piece of object’s state – Events simplify acting on object state changes
  3. Memory Management • Garbage collector provided by Common Language Runtime

    (CLR) • Frees programmer from having to explicitly deallocate memory • Does not eliminate pointers: makes them unnecessary for most tasks
  4. Platform Support • C# programs typically written to run on

    Windows • Microsoft standardized C# language through ECMA • However, few resources available to support C# on non-Windows platforms • Therefore, C# not full replacement for Java • Java best for multiplatform support
  5. Cross-platform Support • May run on server and dish up

    HTML that can run on any platform (ASP.NET) • May run on a runtime other than CLR (Mono Project) • May run on host that supports Silverlight (Flash Player)
  6. C# and the CLR • C# depends on Common Language

    Runtime (CLR) • CLR equipped with host of features – Automatic memory management – Exception handling • Design of C# closely maps to design of CLR • C# type system maps closely to CLR type system
  7. CLR and .NET Framework • .NET Framework consists of CLR

    + vast set of libraries – Core libs – Applied libs (which depend on Core libs) • CLR executes managed code • C# code compiled into managed code • Managed code packaged into assembly (EXE/DLL) along with type info or metadata • Represented in Intermediate Language (IL) • CLR loads IL and converts to native code of machine via CLR JIT (Just-In-Time) compiler
  8. Windows Runtime • WinRT • Execution interface and runtime environment

    • Access libraries in language-neutral and object- oriented fashion • Supports touch-enabled Metro-style applications (Windows Store Apps)
  9. C# Program Structure using System; // Importing namespace class Test

    // Class declaration { static void Main() // Method declaration { int x = 12 * 30; // Statement 1 Console.WriteLine(x); // Statement 2 } // End of method } // End of class
  10. Higher-level Functions using System; class Test { static void Main()

    { Console.WriteLine(FeetToInches(30); Console.WriteLine(FeetToInches(100); } static int FeetToInches (int feet) { int inches = feet * 12; return inches; } }
  11. Creating a Type • Define a Class – Fields –

    Methods – Constructors – Object Initializers – Properties – Indexers – Static Constructors – Static Classes – Finalizers – Partial Types and Methods
  12. Object Initializers Public class Bunny { public string Name; public

    bool LikesCarrots; public bool LIkesHumans; public Bunny () {} public Bunny (String n) { Name = n; } } Bunny b1 = new Bunny { Name = “Bo”, LikesCarrots = true, LikesHumans = false }; Bunny b2 = new Bunny (“Bo”) { LikesCarrots = true, LikesHumans = false };
  13. Properties (Regular Property) public class Stock { decimal currentPrice; public

    decimal CurrentPrice { get { return currentPrice; } set { currentPrice = value; } }
  14. Indexers class Sentence { string[] words = “The quick brown

    fox”.Split(); public string this [int wordNum] { get { return words [wordNum]; } set { words [wordNum] = value; } }
  15. Partial Types and Methods • Allow type definition to be

    split across multiple files partial class PaymentForm { … } • Partial methods let auto-generated partial type provide customizable hooks for manual authoring partial class PaymentForm {
  16. Creating a Type (Continued) • Define a Struct – Value

    type (Class is reference type) – No support for • Inheritance • Parameterless constructors • Finalizers • Virtual members • Structs preferred when value-type semantics desirable – Ex. Numeric types • Define an Interface – Provides specification (contract)
  17. Access Modifiers • Public – Fully accessible • Internal –

    Only within containing assembly or friend assemblies • Private – Only within containing type (default) • Protected – Only within containing type or subclasses
  18. Statements • Expression Statements – Look exactly the same as

    in Java – If – If-else – Switch • Iteration Statements (Loops) – Look exactly the same as in Java – While and do-while – For – Foreach • Supports Jump, Break and Continue and Goto
  19. References • C# 5.0 In a Nutshell, Fifth Edition, by

    Joseph Albahari & Ben Albahari. Published by O’Reilly.