Docsity

C# Programming: Value Types, Reference Types, Boxing, Unboxing, and Parameters - Prof. Aar, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
1
Lecture 12: C# Gotchas
Prof. Aaron Lanterman
School of Electrical and Computer Engineering
Georgia Institute of Technology
2
References
by Ben Albahari,
Peter Drayton, and
Brad Merrill, 2001
Great article:
Jesse Liberty, “Top ten traps in
C# for C++ programmers”
www.ondotnet.com/pub/a/oreilly/dotnet
/news/programmingCsharp_0801.html
3
Value vs. reference types
•Like C++, C# has user defined types
•C# also makes a distinction between value types
and reference types
•Value types:
–Intrinsic types and structs
– “Passed by value” (copied)
–Stored on the stack (unless part of a reference type)
•Reference types:
–Classes and interfaces, and “boxed” value types
– “passed by reference” (implicit pointer)
–Variables sit on the stack, but hold a pointer to an
address on the heap; real object lives on heap
4
Boxing and unboxing
•Boxing allows value types to be treated as
reference types
–Value boxed inside an object
–Unboxed to get original value back
•Everything in C# is derived from “Object,”
so everything can be implicitly cast to an
object
•Unboxing must be done explicitly
pf3
pf4
pf5

Partial preview of the text

Download C# Programming: Value Types, Reference Types, Boxing, Unboxing, and Parameters - Prof. Aar and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Lecture 12: C# Gotchas Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology 2

References

by Ben Albahari, Peter Drayton, and Brad Merrill, 2001 Great article: Jesse Liberty, “Top ten traps in C# for C++ programmers” www.ondotnet.com/pub/a/oreilly/dotnet /news/programmingCsharp_0801.html 3

Value vs. reference types

  • Like C++, C# has user defined types
  • C# also makes a distinction between value types and reference types
  • Value types:
    • Intrinsic types and structs
    • “Passed by value” (copied)
    • Stored on the stack (unless part of a reference type)
  • Reference types:
    • Classes and interfaces, and “boxed” value types
    • “passed by reference” (implicit pointer)
    • Variables sit on the stack, but hold a pointer to an address on the heap; real object lives on heap 4

Boxing and unboxing

  • Boxing allows value types to be treated as

reference types

  • Value boxed inside an object
  • Unboxed to get original value back
  • Everything in C# is derived from “Object,”

so everything can be implicitly cast to an

object

  • Unboxing must be done explicitly

5

Boxing and unboxing

using System; public class UnboxingTest { public static void Main() { int i = 123; //Boxing object o = i; // unboxing (must be explicit) int j = ( int ) o; Console.WriteLine("j: {0}", j); } } If o is null or not an int an InvalidCastException is thrown From www.ondotnet Jesse Liberty,.com/pub/a/ “Top tenoreilly traps/dotnet/news/ in C# for C++programmingCsharp programmers,”_0801.html 6

Structs vs. classes

  • Structs are value types
    • More efficient when used in arrays
    • Less efficient when used in collections
      • Collections expect reference types, so structs must be “boxed” - boxing has overhead
    • Support properties, methods, fields, and operators…
    • …but not inheritance or destructions
  • Classes are reference types
    • May be more efficient when used in collections 7

Reference parameters

  • C, C++, and C# allow a function to only return one value
  • In C++ and C#, you can get around this by passing in pointers
  • In C#:
    • Reference types in the parameter list may be changed by the function
    • To let a function change a value type in the parameter list, can use an explicit ref keyword: public void Changer( ref int x) Aaron.Changer( ref int aaronx); ref must be used in both declaration and call 8 public void Changer( ref int x) int aaronx; Aaron.Changer( ref int aaronx); C# will give a compile-time error

since aaronx has not been

initialized

Variables must be initialized

In general, variables in C# must be assigned before being passed into a function

13

Pop quiz: C

a = 7;

b = 3;

if (a = 5)

b = 10;

  • What is the value of b after this code is run

(assume C code)?

14

Booleans in C#

  • In C, 0 is false, “anything else” is true
  • In C#, this code will give a compile time error
    • C# has distinct Boolean values, true and false

a = 7;

b = 3;

if (a = 5)

b = 10;

15

C# arrays are objects

Java: int arr1[]; C#: int []arr1; arr1 = new int [5]; arr1 = new int [5]{10,20,30,40,50}; int [] arr2 = new int [5] {10,20,30,40,50}; int [] arr2 = {10,20,30,40,50}; 16

Multi-dimensional arrays

string [,] bingo; bingo = new string [3,2] {{“A”,”B”},{“C”,”D”},{“E”,”F”}}; bingo = new string [,] {{“A”,”B”},{“C”,”D”},{“E”,”F”}}; string [,] bingo = {{“A”,”B”},{“C”,”D”},{“E”,”F”}};

17

Jagged arrays

int [][]arr = new int[][] {new int[] {10,11,12}, new int[] {13, 14, 15, 16, 17}};

  • Arrays of arrays 18

Array iteration

int [] arr = {16, 17, 18}; foreach ( int x in arr) { System.Console.WriteLine(x.ToString()); }