1 / 66

A Programmers Introduction to C#

A Programmers Introduction to C#. Keith Elder Microsoft MVP http://keithelder.net/blog/. Assumptions. I assume you Have 1 – 2 years of programming experience Understand the basic concepts of Variables Loops Arrays Can type over 40 words per minute (if you are following along).

sheba
Download Presentation

A Programmers Introduction to C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Programmers Introduction to C# Keith Elder Microsoft MVP http://keithelder.net/blog/

  2. Assumptions • I assume you • Have 1 – 2 years of programming experience • Understand the basic concepts of • Variables • Loops • Arrays • Can type over 40 words per minute (if you are following along)

  3. C# Syntax

  4. All Lines Must End in ; Correct Incorrect int x = 5; int x = 5

  5. Variables Must Declare Type Correct Incorrect int x = 5; x = 5; The keyword “int” is declaring that the value of x going to hold an integer.

  6. Supported C# Types

  7. Type Cannot Be Changed Correct Incorrect int x = 5; int x = 5; x = “foo”; This will result in a compile error. Once a type has been established for a variable, the type cannot change to another data type.

  8. Strings must be in quotes Correct Incorrect string x = foo bar;string x = ‘foo bar’; string x = “foo bar”; char x = “a”; char x = ‘a’;

  9. Operators • C# uses standard mathematical operators • +, -, /, *, <, >, <=, >=, • Expression operators • && • || • == • != • Assignment operators • =, +=, *=, -=, *= http://msdn.microsoft.com/en-us/library/6a71f45d.aspx

  10. Concatenating Strings string x = “foo bar”; string y = x + “ some new string”;

  11. Incorrect Case Sensitive • Correct int x = 5; int Z = 5; int y = x + Z; int x = 5; int Z = 5; int y = x + z;

  12. If / Else if (expression) { } else { } if (expression) { } else if (expression) { } else { } int x = 5; if (x > 5) { x = 10; } else { x = 0; } TIP: If you only have one line in your if block, you can skip the curly braces. int x = 5; If (x % 2 == 0) CallSomeMethod();

  13. Demo – Fun with varibles

  14. Very Simple Program class MyFirstProgram { static void Main() { int x = 5; int y = 10; int z = x + y; } } C# Quick Facts

  15. Breaking Down C# - Namespace Namespace Namespace • Abstract container providing organization to items. • Typically organized by: • Company • Product • Team • Sub-Team • Etc. • Example: • Microsoft.Learning.Csharp Class Fields and Properties Method1 Method2

  16. Breaking Down C# - Namespace namespace SkunkWorks { public class Loan { public decimal loanAmount; public boolisApproved ; } } namespace Vendor { public class Loan { public decimal loanAmount; public boolisApproved ; public intcreditScore ; } }

  17. Demo - Namespaces

  18. Breaking Down A C# - Class • A class is just a blue print for an object. • An object is an allocated region of storage. • An object doesn’t exist until memory is allocated. • A class holds data (properties/fields) and is made up of one or more methods (functions) . Namespace Class Fields and Properties Method1 Method2

  19. Breaking Down C# – Class namespace SkunkWorks { public class Loan { public decimal loanAmount; public boolisApproved; } } Namespace Class Class Fields and Properties Method1 Loan myLoan = new Loan(); Fields Method2 When an instance of a Loan is created in memory, this becomes an object. Thus the variable myLoanis an object.

  20. Breaking Down C# – Class namespace SkunkWorks { public class Loan { public Loan() { } public decimal loanAmount; public boolisApproved; } } Class Class Constructor Field and Properties Constructor Method1 Method2 A constructor is the default “function/method” that executes when a class is instantiated. If one doesn’t exist, a default constructor is provided by the compiler.

  21. Fields and Properties “…. Yeah but I hold the data…” “… Oh yeah…. Well I expose the data…. So nah!”

  22. Breaking Down C# - Fields Fields hold data Field Facts • By default they are “private” to other classes (more later) • Fields hold the data • Placed at the top of classes outside of any methods • Private fields are usually notated with an _ (underscore) prefix • They are named camel cased with first letter starting with a lowerCase

  23. Breaking Down C# - Properties Expose Data Property Facts • Used to expose field data • Used to bind data to user interface elements • Allows for pre and post logic before getting or accessing data • Uses accesorsto set and get field data • Typically always marked public (more later) • Properties always are named in CamelCase, using no underscores or dashes

  24. Properties – Real World Example

  25. Who can see what? • Public means that anyone can access it • Private means that only other members can access it The field docCompletedwill not be able to be viewed from classes outside of Loan. The C# keyword private signifies it is private within the scope of the class itself. Public means it can be seen by other classes.

  26. Accessing Methods and Properties of an Instance • Use the . (period) after the instance name of your object to call methods in the class and access properties. • Intellisense will guide you in VS

  27. Demo – Our first C# Class Creating a class Constructor Fields Properties Public / Private

  28. Breaking Down C# - Methods • Think of methods as actions that classes can perform. • Take a function, put it in a class, we call it a method. • C# doesn’t allow functions to live and breathe outside of a class. Thus, all we have is methods. • Methods can access other methods within the class and properties, and call other methods in other classes. public class Loan { public decimal loanAmount; public boolisApproved; public void MarkApproved() { IsApproved = true; OnMarkedApproved(); } }

  29. Breaking Down C# - Methods Access level Return type Method name Parameters

  30. Demo – Making our classes do something

  31. Using Statements Using statements specify which Namespaces to use to resolve classes. using System; using Vendor; namespace ConsoleApplication { class Program { static void Main() { Loan myLoan = new Loan(); myLoan.LoanAmount = 1000000; } } } One thing we haven’t covered

  32. Breaking Down C# - Static • Referenced through the type not the instance

  33. Breaking Down C# - Static • Can be used with • Fields • Methods • Properties • Operators • Events • Constructors • Cannot be used with indexers and deconstructors

  34. Demo – Using / Static

  35. Working with Classes

  36. Use a Class to Define an Object

  37. A New Object From a Class Is An Instance The variable lassie is the instance of the object. Use the . (period) after the instance of your object to call methods in the class and access properties.

  38. Classes can be re-used

  39. A Primer on Object Oriented Programming Inheritance Encapsulation Abstraction Polymorphism

  40. The Animal Object

  41. Four principles of Object Oriented Programming Inheritance Abstraction Encapsulation Polymorphism

  42. Inheritance – Dogs Gone Wild Inheritance Constructor Although this class doesn’t have FurColor in it, the value is derived from the base class. Rule: Classes that are subclassed inherit all the properties and methods of their parent class.

  43. Subclasses – Different Strokes For Different Folks Subclasses can implement different characteristics. Subclasses can override base class behavior. Adding the keyword “virtual” to a method means it can be overridden in a base class.

  44. Abstraction • Abstraction is simplifying complex reality by modeling classes appropriate to the problem

  45. Encapsulation • Creating an object that keeps track of its state internally using private fields and uses public properties and methods to let other classes work with only the part of the internal data they need to see.

  46. Polymorphism • This word means “Many forms”. • It’s taking an object, and use it in a method or statement that expects something else. Feed takes any Animal. We are down casting the type to Animal.

  47. Interfaces – Taking Abstraction One Step Further • In C#, a class can only inherit from one other class • An Interface is a way of providing abstraction • Interfaces let you define a bunch of methods and properties a class must have

  48. Interfaces – Taking Abstraction One Step Further This class implements the ITremble Interface

  49. Interface Facts Interfaces Quick Facts • Start with an uppercase I • IDisposable, IEnumerable, IList • Any class that implements an Interface has to implement ALL methods and properties • A class can implement one or more Interfaces (separate the names by comma) • VS Shortcut Tip: Alt-Shift-F10 to implement methods

  50. Demo – Interfaces, Abstraction, Inheritance, Oh my!

More Related