Programming in C# Language Overview

Slides:



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

Programming Languages and Paradigms
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Written by: Dr. JJ Shepherd
C# Types Tom Roeder CS fa. Administration CMS is up let me know if you can’t see the course Assignments are posted may not be able to do some.
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++
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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++
History  We first begin with Java which was released in 1995 by Sun Microsystems  Initially Java was 100% interpreted at runtime and was very slow 
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Java versus C# An introduction to C# and Visual Studio.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
1 C++ Syntax and Semantics, and the Program Development Process.
ILM Proprietary and Confidential -
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.
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.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
ITF11006.NET The Basics. Data Types – Value Types – Reference Types Flow Control – Conditional – Loop Miscellaneous – Enumerations – Namespaces Class.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
 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.
Object Oriented Software Development 4. C# data types, objects and references.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Introduction to C# By: Abir Ghattas Michel Barakat.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 C#. 2 C#’s Family Tree C# inherits a rich programming legacy from C (1972) and C++ (1979). It is closely related to Java (1991).
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 –
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
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.
Basic Introduction to C#
C# - OOP TTU IT College , Autumn SEMESTER Andres käver.
Java Primer 1: Types, Classes and Operators
Module 5: Common Type System
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Computing with C# and the .NET Framework
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
Conditional Statements
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Programming in C# Language Overview CSE 459.24 Prof. Roger Crawfis

Overview Quickly go through some of the major differences between C++ or Java and C#. Overview of a C# program. Built-in C# types. Canonical Hello World program

My View on C# I have been using C# in our research for since C# 1.0 in 2004. I think it is a great language!!! However, programming languages are a dime a dozen and somewhat insignificant to large-scale programming. The .NET libraries are the real time savers. Or stated differently – well tested, well maintained and well documented reusable components. I could do everything in C, but view C# as an automatic code-generator for many recurring design and programming patterns. A strongly-typed language prevents many simple typos from being bugs.

Language Namespaces Classes Interfaces (contracts) Control Statements Fields Properties Methods Attributes Events Interfaces (contracts) Control Statements if, else, while, for, switch foreach Additional Features Operation Overloading Structs Enums Delegates OO Features Type Unification Inheritance Polymorphism

C# Program Structure Namespaces Type declarations Members Organization Contain types and other namespaces Type declarations Classes, struct’s, interfaces, enum’s, and delegates Contain members Members Constants, fields, methods, operators, constructors, destructors Properties, indexers, events Organization No header files, types imported using assemblies (dll’s). Type reflection

Major Language Differences Automatic memory management Garbage Collection No pointers Everything inherits from System.Object Additional language constructs to aid in implementing common patterns: Iterators – foreach and yield statements Data encapsulation – Properties Function pointers or Functors – delegates Observer Design Pattern – events Aspect-oriented programming – Attributes (loosely) Functional programming – LINQ and anonymous methods

Other Language Differences Salient points Conditionals must evaluate to a Boolean No pointers. Use the dot “.” to access both namespaces and fields/methods. All fields are initialized by the CLR (zero for value types, null for reference types). The switch statement does not “fall-thru” (unless empty) The switch statement can take bool’s , enum’s, integral types and strings. Expressions must be useful (no a==b;). Internal or assembly protection control.

Built-in Types C# predefined types The “root” object Logical bool Signed sbyte, short, int, long Unsigned byte, ushort, uint, ulong Floating-point float, double, decimal Textual char, string Textual types use Unicode (16-bit characters)

Common Value Types All integral types: int, uint, long, short, byte, … float’s, double’s and decimal’s Structs Aka - user-defined value (light-weight) types Can contain arbitrary data Non-extensible (sealed subclasses) System.Decimal is a struct.

MSDN Documentation Let’s look at the MSDN documentation for ValueType and few other types. Namespace Assembly Language filters Versions

Built-in Types - Strings Strings are first-class immutable objects in C# Behave like a value type Can be indexed like a char array. Has many methods (see System.String) The @ command allows specifying a string literal spanning lines. string s = “Hello”; char third = s[2]; string[] split = s.Split(third);

Instance Intialization Must be initialized (unlike C++) double x; // x == 0 string f; // f.equals(“”) A a; // a == null Fundamental difference between double and class A? Reference types vs. value types

} Reference Types Classes, including user-defined classes Inherited from System.Object Transparently refers to a memory location Similar to pointers in other languages Other view is that: Everything is a pointer. Can be set to null memory a { A a = new A(); A b = a; } } fields in class A for instance a b

Value Types Contain the actual value, not the location Inherited from System.ValueType Which is (logically) derived from System.Object. Treated specially by the runtime Need to be boxed in order to get a reference memory 137 a { int a = 137; int b = a; } 137 b

Boxing and Unboxing Value types are not indirectly referenced More efficient. Less memory. In order to treat all types uniformly, a value type needs is converted to a reference type. called boxing. Reverse is unboxing memory 137 a o1 { int a = 137; object o1 = a; object o2 = o1; int b = (int)o2; } int 137 137 b o2

Differences Between Types Copy semantics: Polynomial a = new Polynomial(); Polynomial b = a; b.Coefficient[0] = 10; Console.WriteLine(a.Coefficient[0]); int a = 1; int b = a; b = 10; Console.WriteLine(a); Copies of value types make a real copy Important for parameter passing Boxing still copies

Function Parameters In C++ you could pass a variable by: Value (creating a new copy and passing it) Pointer (passing in the address) Reference (same as pointer, less cluttered syntax). In C# there seems to be some confusion and misinformation (including C# in a Nutshell) on this. The MSDN documentation for ref has this correct. If you were raised on pointers and understand this (which you are not), then it is quite simple. The ref and out keywords indicate that you should pass: Value types – address of value type is passed. Reference types – address of pointer is passed.

Function Parameters ref parameters Use the value that is passed in Change the value of a value type passed in Change the instance that a reference type points to. A valid (or initialized) instance or value type must be passed in. Null is a valid value.

Function Parameters out parameters Set the value of a value type passed in. Set the instance that a reference type points to. Does not need to be initialized before calling. Compiler forces you to set a value for all possible code paths.

Function Parameters For variable number of parameters public void f(int x, params char[] ar); call f(1), f(1, ‘s’), f(1, ‘s’, ‘f’), f(1, “sf”.ToCharArray()); Must be the last argument in the list

Control Statements If, Else While For Foreach

Switch Statement Switch statements must be expressions that can be statically evaluated. Restricted to primitive types, strings and enums. There is no fall through on switch cases unless the case is an empty case:

Switch Statement switch (myEmployee.Name) { case “Boss”: Console.Writeline(“Your fired!”); break; case “Henry”: case “Jane”: Console.Writeline(“I need a pay raise.”); default: Console.Writeline(“Busy working.”); }

Jump Statements Several C# statements provide a break in the execution: break – jumps out of a while or for loop or a switch statement. continue – starts the next iteration of a loop. goto – do not use. return – returns out of the current method. throw – Causes an exception and ends the current try block or method recursively.

Implicit Type Declarations C# 3.0 feature Aids readability in declaring instances of complex types with redundant initialization: var myStringBuilder = new StringBuilder(); var myList = new List<IDictionary<string,int>>(); Added for LINQ support (anonymous types). Avoid using! Note: still strongly and statically typed

Homework and Quiz Memorize the C# keywords from the appendix in the book that start with the letters a-b. Read the first two chapters of the book. Look over the MSDN documentation for System.String and in particular the Split method.