Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

Slides:



Advertisements
Similar presentations
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Lecture 9 Concepts of Programming Languages
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Java versus C# An introduction to C# and Visual Studio.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Programming Languages and Paradigms Object-Oriented Programming.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
BASE CLASSES AND INHERITANCE CHAPTER 4. Engineer Class.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Introduction to Building Windows 8.1 & Windows Phone Applications.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
ILM Proprietary and Confidential -
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Learners Support Publications Classes and Objects.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Introduction to Object-Oriented Programming Lesson 2.
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Methods. The Structure of a Method Essentially, a method is a block of code with a name. You can execute the code by using the method’s name. You can.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
FASTFAST All rights reserved © MEP Make programming fun again.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
C# for C++ Programmers 1.
Creating and Using Objects, Exceptions, Strings
Creating Your Own Classes
Methods Attributes Method Modifiers ‘static’
C# In many ways, C# looks similar to Java
Lecture 9 Concepts of Programming Languages
Classes and Objects.
Lecture 9 Concepts of Programming Languages
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Introduction to C#

Why C#?

Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)

How Windows and Windows Phone Applications are developed?

Tools Needed

Where to start?

The Basics of C# Lots of similarities with C++ Object-Oriented Classes, structs, enums Familiar basic types: int, double, bool, … Familiar keywords: for, while, if, else, … Similar syntax: curly braces { }, dot notation, … Exceptions: try and catch

The Basics of C# Everything lives in a class/struct (no globals) No pointers! (so no ->, * or & notation) Garbage collection: no delete! No header files Interfaces Static members accessed with. (not ::) In a nutshell: much easier than C++

C# Features Properties Interfaces The foreach keyword The readonly keyword Parameter modifiers: ref and out Delegates and events Generics (Instead of Templates)

Properties Class members, alongside methods and fields “field” is what C# calls a member variable Properties “look like fields, behave like methods” By convention, names are in UpperCamelCase

Properties class Person { // Private field (the "backing field") private String name; // Public property public String Name { get { return name; } set { // "value" is an automatic // variable inside the setter name = value; } class Program { static void Main( string[] args ) { Person p = new Person(); // Use the setter p.Name = "Waseem"; // Use the getter Console.WriteLine( p.Name ); }

Properties Demo

Properties So far, looks like an over-complicated field So why bother?

Properties : Advanced Getter / Setter Can hide implementation detail inside property Hence “looks like a field, behaves like a method” class Person { // Private field (the "backing field") private String name; private static int refCount = 0; // Public property public String Name { get { return name.ToUpper(); } set { name = value; refCount++; }

Properties : Access Modifiers Now only the class itself can modify the value Any object can get the value class Person { // Private field (the "backing field") private String name; // Public property public String Name { get { return name; } private set { name = value; }

Properties : Getter only In this case it doesn’t make sense to offer a setter Can also implement a setter but no getter Notice that Now and Hour are both properties too (of DateTime) – and Now is static! class Thing { // Public property public int CurrentHour { get { return DateTime.Now.Hour; }

Properties : Even simpler example class Person { // If all you want is a simple // getter / setter pair, no need for a // backing field at all public String Name { get; set; } // As you might have guessed, access // modifiers can be used public bool IsWorking { get; private set; } }

Properties A really core feature of C# You’ll see them everywhere DateTime.Now String.Length Etc. Get into the habit of using a property whenever you need a getter and/or setter Preferred to using GetValue(), SetValue() methods Never use public fields!

Interfaces Like a class, but all its members are implicitly abstract i.e., it does not provide any method implementations, only method signatures A class can only inherit from a single base class, but may implement multiple interfaces interface ILog { void Log( string text ); }

Interfaces Demo

foreach Simplified for loop syntax Works with built-in arrays, collection classes and any class implementing IEnumerable interface String implements IEnumerable int[] myInts = new int[] { 1, 2, 3, 4, 5 }; foreach ( int i in myInts ) { Console.WriteLine( i ); }

Foreach Demo

Parameter Modifiers: ref No (explicit) pointers or reference in C# In effect, all parameters are passed by reference But not quite… static void Main( string[] args ) { String message = "I'm Waseem"; Negate( message ); Console.WriteLine( message ); } static void Negate( String text ) { text += " NOT"; } Output

Parameter Modifiers: ref Although it looks like parameter passing is “by reference”, but it actually is “by const reference” The ref keyword fixes this Output static void Main( string[] args ) { String message = "I'm Waseem"; Negate( ref message ); Console.WriteLine( message ); } static void Negate( ref String text ) { text += " NOT"; }

Parameter Modifier: ref Demo

Parameter Modifier: out Like ref but must be assigned in the method static void Main( string[] args ) { DateTime now; if ( IsAfternoon( out now ) ) { Console.WriteLine( "Good Afternoon, it is now " + now.TimeOfDay.ToString() ); } else { Console.WriteLine( "Please come back this afternoon" ); } static bool IsAfternoon( out DateTime currentTime ) { currentTime = DateTime.Now; return currentTime.Hour >= 12; }

Parameter Modifier: out Demo

Delegates Delegates are how C# defines a dynamic interface between two methods Same goal as function pointers in C Delegates are type-safe Consists of 2 parts: a delegate type and a delegate instance

Delegates A delegate type looks like an (abstract) method declaration, preceded with the delegate keyword A delegate instance creates an instance of this type, supplying it with the name of the real method to attach to Example on next slide

Delegates // Delegate type (looks like an abstract method) delegate int Transform( int number ); // The real method we're going to attach to the delegate static int DoubleIt( int number ) { return number * 2; } static void Main( string[] args ) { // Create a delegate instance Transform transform; // Attach it to a real method transform = DoubleIt; // And now call it (via the delegate) int result = transform( 5 ); Console.WriteLine( result ); } I can never remember the syntax for either! Keep a handy reference book…

Multicast Delegates A delegate instance can have more than one function attached to it Now when we call transform(), all methods are called In the order in which they were added Transform transform = DoubleIt; transform += HalveIt; transform += TripleIt; // etc.

Multicast Delegates Methods can also be removed from a multicast delegate transform -= HalveIt;

Delegates Demo

Events Events are really just special instances of delegates By convention they have a specific declaration for the delegate: We declare an event like this We use an event just like a delegate except that only the += and -= can be used by classes delegate void GuestHandler( String name ); event GuestHandler EnterEvent;

Events Demo

using Statement Automatically disposes the object The object must implement IDisposable interface using ( TextWriter w = File.CreateText( "log.txt" ) ) { w.WriteLine( "This is line one" ); }

Questions?

References Microsoft Virtual Academy

Thank You