CNS 3260 C# .NET Software Development

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
BY BOWYA G.  This International Standard is based on a submission from Hewlett-Packard, Intel, and Microsoft, that describes a language called C#. 
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++
Java Syntax Primitive data types Operators Control statements.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
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.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming in C# Language Overview
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
.NET Data types. Introduction ٭ A "data type" is a class that is primarily used just to hold data. ٭ This is different from most other classes since they're.
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Variable, Expressions, Statements and Operators By: Engr. Faisal ur Rehman CE-105 Fall 2007.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
Spring 2007NOEA: Computer Science Programme 1 C# - Introduction Language Fundamentals: Data Types string Objects and Classes Methods Iteration and Selection.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Chapter 2: Using 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.
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.
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.
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
1. 2 Framework Classes and libraries: 3.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Software Development 4. C# data types, objects and references.
1.2 Primitive Data Types and Variables
Session 1 C# Basics.
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.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Operators and Cast. Operators Operator Shortcuts.
Object Oriented Programming Lecture 2: BallWorld.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Basic Introduction to C#
Module 5: Common Type System
Computing with C# and the .NET Framework
5.1 Being Objects and A Glimpse into Coding
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Dr Shahriar Bijani Winter 2017
The Anatomy of a Basic C# Class
Data Types Imran Rashid CTO at ManiWeber Technologies.
Module 2 Variables, Assignment, and Data Types
- This slide is intentionally left blank -
Presentation transcript:

CNS 3260 C# .NET Software Development C# Datatypes CNS 3260 C# .NET Software Development

Types Simple types Complex types int, uint, float, etc. keywords represent structs in IL Complex types Classes Structs Enums Delegates Everything inherits from System.Object

C# Value vs. Reference Value Types “directly contain” the variable data. Example: int, float, enum, struct... Reference Types contain a reference to the data Reference Type Value Type Memory ref0 ref1 Memory int x int y myObject

class ValueType ValueType is a class that inherits directly from object You cannot inherit from ValueType ValueType overrides System.Object methods so they make sense

value-types value-types inherit directly from ValueType simple types enum types struct types All value-types are sealed value-types cannot be null Assignment of a value-type results in a copy of the variable

value-type Hierarchy value-type struct-type enum-type simple-type type-name type-name numeric-type bool integral-type floating-point-type decimal

Simple Types integral-types Signed Unsigned Unicode Type System Type Description Example sbyte System.SByte 8-bit signed integral type sbyte val = 12; short System.Int16 16-bit signed integral type short val = 12; int System.Int32 32-bit signed integral type int val = 12; long System.Int64 64-bit signed integral type long val1 = 12; long val2 = 34L; byte System.Byte 8-bit unsigned integral type byte val1 = 12; ushort System.UInt16 16-bit unsigned integral type ushort val1 = 12; uint System.UInt32 32-bit unsigned integral type uint val1 = 12; uint val2 = 34U; ulong System.UInt64 64-bit unsigned integral type ulong val1 = 12; ulong val2 = 34U; ulong val3 = 56L; ulong val4 = 78UL; char System.Char 16 bit unicode character char c = ‘a’; char c = 255; Signed Unsigned Unicode

Simple Types (Continued) floating-point-types Type System Type Description Example float System.Single Single-precision floating point type float val = 1.23F; double System.Double Double-precision floating point type double val1 = 1.23; double val2 = 4.56D; Decimal Type System Type Description Example decimal System.Decimal Precise decimal type with 28 significant digits decimal val = 1.23M; Boolean Type System Type Description Example bool System.Boolean Boolean type; a bool value is either true or false bool val1 = true; bool val2 = false;

numeric-type Methods static MinValue static MaxValue static Parse static Parse(string) static Parse(string, NumberStyles) ToString ToString() ToString(string format) int index = 256; index.ToString(“X”);

System.Char (remember it’s 16-bit) Has ingetral-type methods, plus... static IsDigit() static IsLetter() static IsDigitOrLetter() static IsLower() static IsNumber() static IsPunctuation() static IsUpper() static ToUpper() static ToLower() and more...

floating-point-types Have numeric-type methods, plus... static Epsilon static NaN static NegativeInfinity static PositiveInfinity static IsInfinity() static IsNaN() static TryParse() and more...

System.Decimal (decimal) Have numeric-type methods, plus... static Floor static GetBits() static ToDouble() static ToSingle() static ToInt64() static Round() static Truncate() and more...

System.Boolean (bool) Have numeric-type methods, plus... static FalseString static TrueString and (not much) more...

structs and enums An enumeration type is a distinct type with named constants. Every enumeration type has an underlying type, which must be byte, sbyte, short, ushort, int, uint, long or ulong. A struct type is a value type that can declare constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types. We’ll cover structs and enums later

Reference Types Classes Interfaces Arrays Delegates object (System.Object) string user-defined classes Interfaces Arrays Delegates delegate (System.Delegate)

reference-type Hierarchy class-type interface-type array-type delegate-type type-name object string

String Type String Type (Immutable reference type) Type System Type Description Example string System.String Immutable string of 16-bit Unicode characters. string s1 = “Hello”; string s2 = string.Empty;

System.String (string) static Empty StartsWith(string) EndsWith(string) IndexOf(string) static Join(string, string[]) Split(char[], int) Remove(int, int) Replace(string, string) Insert(int, string) and much more...

Object Four virtual methods: Two static methods: One protected method: Equals(object obj) GetHashCode() GetType() ToString() Two static methods: static Equals(object objA, object objB) static ReferenceEquals(object objA, objB) One protected method: Finalize()

Object Virtual Methods Equals(object obj) Reference types: the default supports reference-equals Value types: the default supports bitwise equality. Can be overridden GetHashCode() Returns a Hash value GetType() Returns the exact runtime type of the calling instance ToString() Returns a string that represents the calling instance

Object Static Methods Equals(object objA, object objB) This method first determines whether both parameters are null references before calling objA.Equals(objB) ReferenceEquals(object objA, object objB) Returns true if objA is the same instance as objB or if both are null references; otherwise, false

Try this: Q: What’s the difference between Equals and ReferenceEquals? class Class1 { [STAThread] static void Main(string[] args) Console.WriteLine(object.Equals(null, null)); Console.WriteLine(object.ReferenceEquals(null, null)); Console.ReadLine(); } Q: What’s the difference between Equals and ReferenceEquals? A: Equals calls objA.Equals(objB). So if objA redefined .Equals(), that overridden method will be invoked. ReferenceEquals will always tell if the references are the same.

Everything derives from object 3.ToString(); “hello”.GetHashCode(); 5.Equals(5); myObject.Equals(yourObject);

Boxing Value Types which are up-cast to a reference type are boxed This is necessary because Collections hold objects Boxing places a copy of the variable in the object Extra information (such as type info) may be stored with the variable, the standard is not explicit

Boxing example class Test { static void Main() int i = 123; object o = i; // boxing int j = (int) o; // unboxing }

Type Conversions Implicit Conversions Implicit conversions will occur when there is no loss of information. Example: using System; class Test { static void Main() int intValue = 123; long longValue = intValue; Console.WriteLine("{0}, {1}", intValue, longValue); }

Conversions Explicit Conversions Explicit conversions are required when there could be loss of information. Example: using System; class Test { static void Main() long longValue = System.Int64.MaxValue; int intValue = (int)longValue; Console.WriteLine(“(int){0} = {1}", longValue, intValue); }

Conversions Explicit or Implicit? float to Int32? Explicit Int32 to float? Implicit decimal to float? Explicit long to decimal? Implicit Int32 to UInt32? Explicit UInt32 to Int32? Explicit (See Conversions Demo)

Local variables must be assigned before they are used no static locals no const locals Example: void fun() { int a, b = 3; int c = a + b; }

Fields static instance public class Rectangle { public int Top = 0; public int Left = 0; public int Height = 100; public int Width = 100; private static int RectangleCount; public Polygon() { ++ RectangleCount; } public ~Polygon() { -- RectangleCount; } }

Local vs. Field class SomeClass { private int index; public SomeClass(int index) { index = index; // WRONG!!! this.index = index; }

Pointers? only in unsafe blocks (we’ll explore these later.)

Passing Parameters: Value class MainClass { static void Main() int i = 0; new MainClass().fun(i); Console.WriteLine(i.ToString()); Console.ReadLine(); } private void fun(int x) x = 5; What does it mean to pass a reference by value? (See PassReference Demo)

Passing references Passing object to a method only passes reference Fast Easy Somewhat misunderstood No implicit deep copy if needed

Passing Parameters: ref class MainClass { static void Main() int i = 0; new MainClass().fun(ref i); Console.WriteLine(i.ToString()); Console.ReadLine(); } private void fun(ref int x) int y = x; // ok x = 5; int z = x; // ok If a reference type is being passed, then it is a reference to the reference.

Passing Parameters: out Similar to passing by ref, but the value is initially unassigned. Also, you must assign to the parameter somewhere in the method. class MainClass { static void Main() int i = 0; new MainClass().fun(out i); Console.WriteLine(i.ToString()); Console.ReadLine(); } private void fun(out int x) // int y = x; // not ok x = 5; int z = x; // ok

Variable Length Arguments Uses params keyword. class Test { static void F(params int[] args) Console.WriteLine("# of arguments: {0}", args.Length); for (int i = 0; i < args.Length; i++) Console.WriteLine("\targs[{0}] = {1}", i, args[i]); } static void Main() F(); F(1); F(1, 2); F(1, 2, 3); F(new int[] {1, 2, 3, 4});

Intro to delegates Think of a delegate as a function pointer that stores a reference to its instance. C++ function pointer: int* (*funPtr)(char arg1, float arg2); C# delegate: public delegate void funDelegate(char arg1, float arg2);

Delegate Syntax Delegate declaration declares a new type. public delegate string FunDelegate(int a, float b); public class MainClass { [STAThread] static void Main() FunDelegate fd = new FunDelegate(fun); fd(5, 3.14159); } private string fun(int x, float f) return (x+f).ToString();

Delegates Why use delegates (or function pointers?) Keeps coupling low Makes code more extensible Asynchronous processing (See SimpleDelegateDemo)