660 likes | 789 Views
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).
E N D
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)
All Lines Must End in ; Correct Incorrect int x = 5; int x = 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.
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.
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’;
Operators • C# uses standard mathematical operators • +, -, /, *, <, >, <=, >=, • Expression operators • && • || • == • != • Assignment operators • =, +=, *=, -=, *= http://msdn.microsoft.com/en-us/library/6a71f45d.aspx
Concatenating Strings string x = “foo bar”; string y = x + “ some new string”;
Incorrect Case Sensitive • Correct int x = 5; int Z = 5; int y = x + Z; int x = 5; int Z = 5; int y = x + z;
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();
Very Simple Program class MyFirstProgram { static void Main() { int x = 5; int y = 10; int z = x + y; } } C# Quick Facts
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
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 ; } }
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
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.
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.
Fields and Properties “…. Yeah but I hold the data…” “… Oh yeah…. Well I expose the data…. So nah!”
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
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
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.
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
Demo – Our first C# Class Creating a class Constructor Fields Properties Public / Private
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(); } }
Breaking Down C# - Methods Access level Return type Method name Parameters
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
Breaking Down C# - Static • Referenced through the type not the instance
Breaking Down C# - Static • Can be used with • Fields • Methods • Properties • Operators • Events • Constructors • Cannot be used with indexers and deconstructors
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.
A Primer on Object Oriented Programming Inheritance Encapsulation Abstraction Polymorphism
Four principles of Object Oriented Programming Inheritance Abstraction Encapsulation Polymorphism
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.
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.
Abstraction • Abstraction is simplifying complex reality by modeling classes appropriate to the problem
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.
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.
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
Interfaces – Taking Abstraction One Step Further This class implements the ITremble Interface
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