Docsity

Distinction Between Value and Reference Types | ECE 4893, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Distinction Between Value and Reference Types | ECE 4893 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

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

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

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 Jesse Liberty, “Top ten traps in C# for C++ programmers,” www.ondotnet.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html

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

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

The out keyword

public void Changer(out int x) int aaronx; Aaron.Changer( out int aaronx);

out keyword like ref , except it tells C# that

it’s OK for the value to be undefined

C# will demand that you assign aaronx

before the function returns!

C# Finalizers

~MyClass() { // your code to release unmanaged resources // used by object } is syntactic sugar for ~MyClass.Finalize { // your code to release unmanaged resources ///used by object base.Finalize(); } Your finalizer should not try to deal with other C# reference objects - only deal with unmanaged resources! Adapted from Jesse Liberty, “Top ten traps in C# for C++ programmers,” www.ondotnet.com/pub/a/oreilly/dotnet/news/programmingCsharp_0801.html

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)?

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;

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”}};

Jagged arrays

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

  • Arrays of arrays