Download presentation
Presentation is loading. Please wait.
1
(to be given after OOP lectures)
INF230 Basics in C# Programming AUBG, COS dept Lecture 29 Structs (to be given after OOP lectures) Reference: Doyle, chap 11
2
Lecture Contents: Structs – value types Structs – configuration
Structs and Classes - comparison
3
Chapter 11 Advanced Object-Oriented Programming Features
C# Programming: From Problem Analysis to Program Design 4th Edition
4
Predefined Data Types Common Type System (CTS)
Divided into two major categories Figure NET common types C# Programming: From Problem Analysis to Program Design
5
Value and Reference Types
Figure 2-4 Memory representation for value and reference types C# Programming: From Problem Analysis to Program Design
6
Value Types Fundamental or primitive data types
Figure 2-5 Value type hierarchy C# Programming: From Problem Analysis to Program Design
7
Chapter 9 John Sharp C# Programming 4th Edition
8
Working with Structures
Classes define reference types that are always created on the heap. A structure is a value type created on the stack. Like a class, structure can have its own fields, methods and constructors (with an important exception - see next slide)
9
Important exception C# compiler message:
Structs cannot contain explicit parameterless constructors
10
Important exception C# does not allow structs to have a default parameterless constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. C# compiler disallows definition of a default constructor by the user.
11
Common Structures Types
FYI: Primitive numeric types int, long, float are aliases for the following structures System.Int32, System.Int64, System.Single These structures have own fields and methods. For example, int i=99; Console.WriteLine(i.toString());
12
Declaring Structures struct Book { public string Author;
public string Title; public string Publisher; public int NumPages; public double Price; public Book(string pa, string pb, string pc, int pd,double pe) { Author = pa;Title = pb;Publisher = pc;NumPages = pd;Price = pe; } public void printStruct() Console.WriteLine("{0} {1} {2} {3} {4}", Author, Title, Publisher, NumPages, Price); }; // end of structure
13
Declaring Structure Variables
Book myBook; // stack located myBook.Author = "lin & Bon"; myBook.Title = "Formal Languages & Language Processors"; myBook.Publisher = "TU SOfia"; myBook.NumPages = 190; myBook.Price = 5.0;
14
Declaring Structure Variables
Book myBook = new Book(); // stack located myBook.Author = "lin & Bon"; myBook.Title = "Formal Languages & Language Processors"; myBook.Publisher = "TU SOfia"; myBook.NumPages = 190; myBook.Price = 5.0;
15
Declaring Structure Variables
Book linBook = new Book ( "L.Nikolov", "Operating Systems", "Ciela", 210, 12.8 );
16
Understanding Structure initialization
Time now = new Time(); //Initialized to 0 Time now = new Time(6,10,45); //Initialized Time now; uninitialized
17
Copying Structure Variables
Time now = new Time(6,10,45); Time copy = now;
19
Structures in C# Compile, Run and Test attached demo program.
20
Structures in C# Modify the code: Book myBook = new Book(); To
Compile, Run and Test
21
Structures in C# C# structures are similar to C# classes.
Here are two examples. The first example shows you how to declare and use structs. The second example demonstrates the difference between structs and classes when instances are passed to methods. You are also introduced to the following topics: •Structs vs. Classes •Heap or Stack? •Constructors and Inheritance
22
Structures in C# Example 1
This example declares a struct with three members: a property, a method, and a private field. It creates an instance of the struct and puts it to use:
23
Structures in C# struct SimpleStruct { private int xval; public int X
get return xval; } set if (value < 100) xval = value; public void DisplayX() Console.WriteLine("The stored value is: {0}", xval);
24
Structures in C# class TestClass { public static void Main()
SimpleStruct ss = new SimpleStruct(); ss.X = 5; ss.DisplayX(); }
25
Structures in C# Output ???
26
Structures in C# Output The stored value is: 5
27
Structures in C#: Structs vs. Classes
Structs may seem similar to classes, but there are important differences First of all, classes are reference types and structs are value types. Heap or Stack? When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.
28
Structures in C# Example 2
This example shows that when a struct is passed to a method, a copy of the struct is passed, but when a class instance is passed, a reference is passed.
29
Structures in C# class TheClass { public int x; } struct TheStruct
30
Structures in C# class TestClass {
public static void structtaker(TheStruct s) s.x = 5; } public static void classtaker(TheClass c) c.x = 5; public static void Main() TheStruct a = new TheStruct(); TheClass b = new TheClass(); a.x = 1; b.x = 1; structtaker(a); classtaker(b); Console.WriteLine("a.x = {0}", a.x); Console.WriteLine("b.x = {0}", b.x);
31
Structures in C# Output ???
32
Structures in C# Output a.x = 1 b.x = 5
33
Structures in C# Output a.x = 1 b.x = 5 Code Discussion
The output shows that only the value of the class field was changed when the class instance was passed to the classtaker( ) method. The struct field, however, did not change by passing its instance to the structtaker() method. This is because a copy of the struct was passed to the structtaker() method, while a reference to the class was passed to the classtaker() method.
34
Structures in C# Structs and Constructors
Structs can declare constructors, but they must take parameters. It is an error to declare a default (parameterless) constructor for a struct. Struct members cannot have initializers. A default constructor is always provided to initialize the struct members to their default values. When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator. If you do not use New, the fields will remain unassigned and the object cannot be used until all the fields are initialized.
35
Structures in C# Structs and Inheritance
There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class object. A struct can implement interfaces, and it does that exactly as classes do. Here's a code snippet of a struct implementing an interface:
36
Structures in C# interface IImage { void Paint(); }
struct Picture : IImage public void Paint() // painting code goes here private int x, y, z; // other struct members
37
Thank You For Your Attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.