Structs.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Windows Programming Using C# Classes & Interfaces.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Classes, Interfaces and Packages
Introduction to C# By: Abir Ghattas Michel Barakat.
Classes Single inheritance Single inheritance Multiple interface implementation Multiple interface implementation Class members Class members  Constants,
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
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.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 2 Objects and Classes
Procedural and Object-Oriented Programming
(to be given after OOP lectures)
C# for C++ Programmers 1.
Creating and Using Objects, Exceptions, Strings
Classes C++ representation of an object
Java Yingcai Xiao.
2.7 Inheritance Types of inheritance
Module 5: Common Type System
Object-Oriented Programming
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
How to Create and use Classes and Structures
Object-Oriented Programming Using C++
Chapter 2 Objects and Classes
C# In many ways, C# looks similar to Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pass by Reference, const, readonly, struct
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Using Classes and Objects
CS212: Object Oriented Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Pointers, Dynamic Data, and Reference Types
Interfaces.
How to Create and use Classes and Structures
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Object-Oriented Programming Using C++
Chapter 14 Abstract Classes and Interfaces
Object Oriented Programming in java
CIS 199 Final Review.
Java Programming Language
5. 3 Coding with Denotations
Classes C++ representation of an object
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Structs

Structs vs Classes Both are user-defined types Both can implement multiple interfaces Both can contain Fields, constants, events, arrays Methods, properties, indexers, operators, constructors Both can have Private Fields Both can have Public Properties Both can have constructors Both can have methods System.Int32 is a struct (ex.)

Classes and Structs class CPoint { int x, y; ... } struct SPoint { int x, y; ... } CPoint cp = new CPoint(10, 20); SPoint sp = new SPoint(10, 20); 10 sp 20 cp CPoint 10 20

Class Struct Reference type (holds a reference to an object in memory) Value type (holds value in memory where it is declared) Can inherit from any non-sealed reference type No inheritance (inherits only from System.ValueType) Can have a destructor No destructor Can have user-defined parameterless constructor No user-defined parameterless constructor Destructors: http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

Class Struct Only reference destroyed immediately after scope is lost. Objects are removed by GC Destroyed immediately after scope is lost When copy, only get a copy of reference When copy, a new copy is created Can be sealed or not Implicitly sealed (no one can extend them)

C# Structs vs. C++ Structs (very different) Same as C++ class, but all members are public User-defined value type Can be allocated on the heap, on the stack or as a member (can be used as value or reference) Always allocated on the stack or in-lined as a member field Members are always public Members can be public, internal or private

StructDemo struct SimpleStruct { private int id; private string name; public int ID get {return this.id;} set { this.id = value; } } public string Name get { return this.name; } set { this.name = value; } public SimpleStruct(int id, string name) : this() this.ID = id; this.Name = name; public override string ToString() return "ID= " + ID + ", Name= " + Name;

Stack and Heap

Destructors (only in classes) A destructor in C# overrides System.Object.Finalize method. You have to use destructor syntax to do so. Manually overriding Finalize will give you an error message. Basically what you trying to do with your Finalize method declaration is hiding the method of the base class. It will cause the compiler to issue a warning which can be silenced using the new modifier (if it was going to work). The important thing to note here is that you can't both override and declare a new member with identical name at the same time so having both a destructor and a Finalize method will result in an error (but you can, although not recommended, declare a public new void Finalize() method if you're not declaring a destructor). ~Customer() { } No argument Constructor (only in classes)

Enums Strongly typed Can specify underlying type No implicit conversions to/from int Operators: +, -, ++, --, &, |, ^, ~ Can specify underlying type byte, short, int, long enum Color: byte { Red = 1, Green = 2, Blue = 4, Black = 0 }

Delegates Object oriented method(function) pointers Multiple receivers Each delegate has an invocation list Thread-safe + and - operations Foundation for events delegate void MouseEvent(int x, int y); delegate double MyMethod(double x); MyMethod m= new MyMethod(Math.Sin); double x = m(1.0);

Delegates A delegate is similar to a pointer to a function C/C++. Since pointers are undesirable in C# programming the delegate is the proper alternative. A delegate is a prototype for a method. Instantiate a delegate and set it to reference one or more methods to be called when it is invoked.

Unified Type System Everything is an object All types ultimately inherit from object Any piece of data can be stored, transported, and manipulated with no extra work object Stream Hashtable int double MemoryStream FileStream