C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
BY BOWYA G.  This International Standard is based on a submission from Hewlett-Packard, Intel, and Microsoft, that describes a language called C#. 
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 14: Overloading and Templates
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
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++
Lecture 9 Concepts of Programming Languages
Reflection, Conversions, and Exceptions Tom Roeder CS fa.
Operator Overloading in C++
1 CSC241: Object Oriented Programming Lecture No 07.
Review of C++ Programming Part II Sheng-Fang Huang.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Programming Languages and Paradigms Object-Oriented Programming.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
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.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition 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.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
Object Oriented Software Development
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 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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Attributes C#.Net Software Development Version 1.0.
 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.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Introduction to C# By: Abir Ghattas Michel Barakat.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
1 CSC241: Object Oriented Programming Lecture No 08.
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 –
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
C# for C++ Programmers 1.
3 Introduction to Classes and Objects.
C# Operator Overloading and Type Conversions
2.7 Inheritance Types of inheritance
Java Primer 1: Types, Classes and Operators
Methods Attributes Method Modifiers ‘static’
Computing with C# and the .NET Framework
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Lecture 22 Inheritance Richard Gesick.
Classes & Objects: Examples
CIS 199 Final Review.
Chapter 5 Classes.
Presentation transcript:

C# Structs, operator overloading & attributes

Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions. Structs are different from classes: variables of a struct type directly contain the value, whereas variables of a class type contain a reference to the value (i.e. the object).

Structs 2 Structs are value types and are said to have value semantics. struct Point { public int x, y; public Point(int x, int y) { this.x = x; this.y = y; }

Structs 3 Classes are reference types and are said to have reference semantics. Point is a struct: 10; Point is a class: 100 Point a = new Point(10, 10); Point b = a; a.x = 100; System.Console.WriteLine(b.x);

Constructing structs All struct types implicitly inherit directly from System.ValueType, and are never abstract. A struct is not permitted to declare a parameterless instance constructor; every struct has one implicitly. The default value of a struct is the value produced by setting all its value type fields to their default value and all its reference type fields to null. The struct value itself cannot be null.

Operators & operands Expressions are constructed from operands and operators. The operators of an expression indicate which operations to apply to the operands. Examples of operators include +, -, *, /, and ‘new’. Examples of operands include literals, fields, local variables, and expressions.

Operators Three kinds of operators: –Unary operators. The unary operators take one operand and use either prefix notation (such as –x ) or postfix notation (such as x++ ). –Binary operators. The binary operators take two operands and all use infix notation (such as x + y ). –Ternary operator. Only one ternary operator, ?:, exists; it takes three operands and uses infix notation (c ? x : y ).

Operator overloading 1 Certain operators can be overloaded. (Cannot overload e.g. =, &&, ||, ?:, method invocation or member access) Operator overloading permits user- defined operator implementations to be specified for operations

Operator overloading 2 User-defined operator declarations always require at least one of the parameters to be of the class or struct type that contains the operator declaration. User-defined operator declarations cannot modify the syntax, precedence, or associativity of an operator.

Operator overloading example using System; public struct Digit { byte value; public Digit(byte value) { this.value = value; } public Digit(int value): this((byte) value) {} public static Digit operator+(Digit a, Digit b) { return new Digit(a.value + b.value); } public static Digit operator-(Digit a, Digit b) { return new Digit(a.value - b.value); } public static bool operator==(Digit a, Digit b) { return a.value == b.value; } … }

Attributes C# programmers can attach attributes to various program entities, and retrieve attribute information at run-time. Principally analogous to well-known declarations of program entities, e.g. method access: public, protected, and private. Attributes are accessible at run-time in a “reflective” manner.

Attributes 2 A class that derives from the abstract class System.Attribute is an attribute class. The declaration of an attribute class defines a new kind of attribute that can be placed on a declaration. By convention, attribute classes are named with a suffix of ‘Attribute’.

Attribute parameters Attribute classes can have positional parameters and named parameters. Each public instance constructor for an attribute class defines a valid sequence of positional parameters for that attribute class. Each non-static public read-write field and property for an attribute class defines a named parameter for the attribute class.

Attribute example using System; [AttributeUsage(AttributeTargets.Class)] public class HelpAttribute: Attribute { public HelpAttribute(string url) { // url is a positional parameter } public string Topic { // Topic is a named parameter get {…} set {…} } public string Url { get {…} } }

Attribute example (cont.) This Help attribute class might be used as follows: [Help(" class Class1 { … } [Help(" Topic ="Class2")] 1 class Class2 { … }

Querying attributes using System; class Test { static void Main() { Type type = typeof(Class1); object[] arr = type.GetCustomAttributes(typeof(HelpAttribute), true); if (arr.Length == 0) Console.WriteLine("Class1 has no Help attribute."); else { HelpAttribute ha = (HelpAttribute) arr[0]; Console.WriteLine("Url = {0}, Topic = {1}", ha.Url, ha.Topic); }

Using System; class Test { static void Main() { string s = "Test"; string t = string.Copy(s); Console.WriteLine(s == t); Console.WriteLine((object)s == (object)t); } Produces: True False

Design goals Simple, modern, general-purpose, object- oriented programming language. Software robustness, durability, and programmer productivity are important. Not intended to compete directly on performance and size with C or assembly language.

Hello, World Source stored in one or more text files with a file extension of.cs compiled with a command line like csc hello.cs using System; class Hello { static void Main() { Console.WriteLine("hello, world"); }

Language details C# is a standard proposal from ECMA Technical Committee 39, Task Group 2 (TG2) C# does not contain a standard class library Common Language Infrastructure (CLI) is a standard for a library and execution environment CLI is a standard proposal from Task Group 3

Unified type system using System; class Test { static void Main() { Console.WriteLine(3.ToString()); }

Boxing/unboxing ” This type system unification provides value types with the benefits of object-ness without introducing unnecessary overhead. ” class Test { static void Main() { int i = 123; object o = i; // boxing int j = (int) o; // unboxing }

Casting Implicit casting Explicit casting

Example of reference passing using System; class Test { static void Swap(ref int a, ref int b) { int t = a; a = b; b = t; } static void Main() { int x = 1; int y = 2; Console.WriteLine("pre: x = {0}, y = {1}", x, y); Swap(ref x, ref y); Console.WriteLine("post: x = {0}, y = {1}", x, y); } Produces:pre: x = 1, y = 2 post: x = 2, y = 1 2

Parameters Value parameter Reference parameter Output parameter Parameter array

Value parameters Used for ’in’ passing to a method Value is copied to method’s scope

Example of value passing using System; class Test { static void F(int p) { Console.WriteLine("p = {0}", p); p++; } static void Main() { int a = 1; Console.WriteLine("pre: a = {0}", a); F(a); Console.WriteLine("post: a = {0}", a); } Produces:pre: a = 1 p = 1 post: a = 1

Example of output parameters using System; class Test { static void Divide(int a, int b, out int result, out int remainder) { result = a / b; remainder = a % b; } static void Main() { for (int i = 1; i < 10; i++) for (int j = 1; j < 10; j++) { int ans, r; 18 Divide(i, j, out ans, out r); Console.WriteLine("{0} / {1} = {2}r{3}", i, j, ans, r); }

Properties & Indices

Delegates