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++

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Microsoft.NET Fundamentals. Introduction Name Company affiliation Title/function Job responsibility Database & Developer experience Your expectations.
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
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
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#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
Terms and Rules Professor Evan Korth New York University (All rights reserved)
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming in C# Language Overview
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.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Programming in C#. I. Introduction C# (or C-Sharp) is a programming language. C# is used to write software that runs on the.NET Framework. Although C#
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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:
JAVA BASICS Prepared by The Smartpath Information Systems
Introduction to Building Windows 8.1 & Windows Phone Applications.
Introduction to Exception Handling and Defensive Programming.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
1 C# A brief overview by Jack Senechal and Bryan Powell.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
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)
Programming in Java CSCI-2220 Object Oriented Programming.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Introduction to C# By: Abir Ghattas Michel Barakat.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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 –
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Intro to Java L. Grewe.
C# for C++ Programmers 1.
Creating and Using Objects, Exceptions, Strings
Java Primer 1: Types, Classes and Operators
Computing with C# and the .NET Framework
CS360 Windows Programming
C# In many ways, C# looks similar to Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Java Programming Language
Conditional Statements
Presentation transcript:

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++

.NET Multiple languages which can interoperate Languages compile to a common intermediate language Common Language Runtime runs programs from all the.NET languages

C# versus Java Both were designed to be improvements on C and C++ –Java aims to be one language for many platforms portable (platform-independent) safe –C# aims to provide many languages for a single platform power unsafe code is allowed not platform-independent

Language Features Strong type checking Array bounds checking Detection of uninitialized variables Automatic garbage collection Designed for use in distributed environments Supports internationalization

First Program namespace FirstProgram { class First { static void Main() { System.Console.WriteLine( "Welcome to C#!"); }

C# on onyx Workstations will have a Windows virtual machine with C# Express installed

C# Express VMWare should get installed on all the workstations in the lab –Not on the server –Virtual machine is frozen you have to transfer any files you create onto onyx –Use SSH Secure File Copy program

Getting your own copy C# Express –Free from Microsoft – Development Environment –Available to you through MSDN Alliance –details later

Keywords (reserved) abstractasbaseboolbreakbytecase catchcharcheckedclassconstcontinuedecimal defaultdelegatedodoubleelseenumevent explicitexternfalsefinallyfixedfloatfor foreach gotoifimplicitinintinterface internalis locklongnamespacenewnull object operatoroutoverrideparamsprivateprotected publicreadonlyrefreturnsbytesealedshort sizeofstackallocstaticstringstructswitchthis throwtruetrytypeofuintulongunchecked unsafeushortusingvirtualvolatilevoidwhile

C# Program Structure A program consists of one or more files A file can contain one or more classes and/or namespaces –name of file is not tied to name of class At least one class must contain Main –There are several allowed signatures return type is either int or void either no parameter or String array

C# Types Unified type system –Everything can be treated as an object Value types –simple types: primitive types from Java plus unsigned types and decimal –structs Reference types - like object references in Java Pointer types - used only in unsafe code

Operators Java operators with similar precedence Extra operators –typeof - returns class name (getClass()) –checked/unchecked (overflow) –is (like instance of) –as (returns cast value or null) Unsafe operators –*, &, sizeof

Operators: differences from Java C# allows you to overload operators == compares values for strings and simple types, addresses for all other objects

Console I/O System.Console.WriteLine( arg) –argument can be any type –for objects, ToString is invoked System.Console.ReadLine() –returns a String System is a namespace –using System; allows you to omit the namespace when calling the method

Namespaces Sort of like a package –in Java, package statement applies to everything in the file –can import all or part of a package A single C# file can contain classes from different namespaces –using imports entire namespace

C# Class Members Function Members –Instance constructor –Static constructor –Method –Property (accessor/mutator) –Indexer –Operator –Destructor (finalizer) –Nested types (inner classes) Data Members –Field instance static –Constant –Read-only –Delegate –Event

C# Class Modifiers new abstract sealed (final) access modifiers –public –protected –internal –private

Inheritance Single inheritance (System.Object) class : {… } Interfaces provide restricted form of multiple inheritance interface {… } –Syntax for implementing is same as for inheritance –Multiple interfaces separated by comma –Super class, if any, must be first in list

Methods Same syntax as Java Default access is private (rather than package) Value types are passed by value usually –ref keyword used to pass by reference –out keyword provides for return values Reference types behave as in Java

Properties C# properties provide the same service as Java's accessors and mutators public string Color { String MyColor; get {return MyColor;} set { MyColor = value; } } Use the name Color to get value of or assign value to private instance variable MyColor

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 };

Control Statements do, while, for, if-else same as in Java foreach provides loop for arrays switch-case like Java except –string case values are allowed –no fall-through behavior use goto case instead C# has goto statement

String string is alias for System.String reference type –equality operators use value semantics Literals are written with double quotes around them –use \ for escape character Verbatim string preceded –escape character not supported –can span multiple lines string verbatim verbatim string with a "" in it"

Arrays Arrays are objects –Subclasses of System.Array –Reverse, Sort, IndexOf methods Declaration, instantiation, initialization, element access as in Java for 1D arrays int []MyArray = new int[5]; –Length property gives the number of elements –Only this syntax is allowed

Arrays For rectangular 2D arrays int [,] Array2D = new int[2,5]; –Length property gives total number of elements –Element access is the same as in Java For jagged arrays int [][] JaggedArray2D = new int[2][]; JaggedArray[0] = new int[3];

Structs A struct is very similar to a class except –structs are value types; classes are reference types –no inheritance –struct has a default constructor always –all constructors must initialize all data –can be "instantiated” without new

Exceptions System.Exception try-catch-finally has same syntax as Java –Don't need a variable name in catch if you aren't going to use it –Within catch block, throw throws the exception that was caught All exceptions are unchecked

Delegates Delegate is a special type that encapsulates one or more methods public delegate int MyDelegate( string s, int i); Instantiate with a method that has the same signature and return type MyDelegate d2 =new MyDelegate(obj.ObjectMethod);

Events C# event model is similar to Java - delegation based An Event is a special type of Delegate Event is created by event source when an event occurs. The Event is passed to the event consumer's handler Handlers must be registered with the event source

etc. Operator overloading Reflection - asking a class for information about it Attributes –Used to add annotation to a class that can be read using reflection Preprocessor Unsafe code

mono mono is an open-source project that provides facilities for running C# programs under Linux Compile a program by typing mcs First.cs Run a program by typing mono First.exe

Reference Material Safari books at Albertson's Library –C# in Depth –Learning C# –Microsoft Visual C# Step by Step –Programming C#