User-Defined Value Types (Structures)
Applications often require types to encapsulate essentially numeric quantities such as currencies, screen coordinates, and temperatures,
which are not represented by the available primitive types.
Using classes in these scenarios would be like using a hammer to crack a nut; the run-time overhead for garbage-collecting these simple objects would be unnecessarily high.
The .NET Framework provides user-definable value types as a solution to this problem.
In C#, a value type is written as a struct. Remember that like value types, instances of structs are stored wherever they are used.
using System;
struct Money
{
// private instance field
private int centsAmount;
// private class field
private const string currencySymbol = "$";
// public constructor
public Money(int dollars, int cents)
{
centsAmount = (dollars * 100) + cents;
}
// another public constructor
public Money(double amount)
{
centsAmount = (int)((amount * 100.0) + 0.5);
}
}
class MyClass
{
static void Main()
{
Money freebie;
Money salary = new Money(20000, 0);
Money carPrice = new Money(34999.95);
}
}
Although structures cannot explicitly inherit from an arbitrary class, they can implement interfaces. For example, it is quite common to implement standard .NET Framework interfaces such as IComparable, which allows us to specify how objects should be compared, and so enables sorting. Value types will often implement this to interoperate well with other classes in the .NET Framework.
We'll be covering interfaces in more detail later in this chapter, but the following preview demonstrates how easily we can change our Money value type to implement an interface, and override the ToString() method inherited from System.Object:
// value_type_inheritance.cs
using System;
struct Money : IComparable
{
// private fields
private int centsAmount;
private const string currencySymbol = "$";
// public constructors
public Money(int dollars, int cents)
{
centsAmount = (dollars * 100) + cents;
}
public Money(double amount)
{
centsAmount = (int)((amount * 100.0) + 0.5);
}
// compare with another Money
public int CompareTo(object other)
{
Money m2 = (Money)other;
if (centsAmount < m2.centsAmount)
return -1;
else if (centsAmount == m2.centsAmount)
return 0;
else
return 1;
}
// return value as a string
public override string ToString()
{
return currencySymbol + (centsAmount / 100.0).ToString();
}
}
Enumerations
Enumerations are .NET value types that represent integral types with a limited set of permitted values. They may also be used to map bit flags onto an integer type to allow a convenient way to represent a combination of options using a single variable. Enumerations are present in many programming languages, but in .NET they are also object-oriented. This means that developers now have access to additional features, which are not present in other languages.
Enumerated Types
To declare an enumerated type, we use the enum keyword and specify symbolic names to represent the allowable values. We can also specify an underlying integral data type to be used for the enumeration (byte, short, int, or long), and optionally assign a specific number to each of the names.
The following example, enumerations.cs, declares a simple enumeration to represent medals in a competition:
// enumerations.cs
using System;
enum Medal : short
{
Gold,
Silver,
Bronze
}
Enumerations inherit implicitly from System.Enum, and so inherit all of its members. Having defined an enumerated type, we can use it in our code as follows:
class MyClass
{
static void Main()
{
Medal myMedal = Medal.Bronze;
Console.WriteLine("My medal: " + myMedal.ToString());
}
}
Medal[] medals = (Medal[])Enum.GetValues(typeof(Medal));
foreach (Medal m in medals)
{
Console.WriteLine("{0:D}\t{1:G}", m, m);
}
class MyClass
{
// Field (usually private, for encapsulation)
private int aField;
// Property (usually public, for ease of use)
public int AProperty
{
get { return aField; }
set { aField = value; }
}
// Constant (class-wide, read-only field)
private const int aConstant = 43;
// Delegate (defines a method signature)
public delegate void ADelegate(int aParameter);
// Event (to alert event receiver objects)
public event ADelegate AnEvent;
// Method
public void AMethod()
{
// Method implementation code
}
// Instance constructor (to initialize new objects)
public MyClass(int aValue)
{
aField = aValue;
}
// Destructor method (to tidy up unmanaged resources)
~MyClass()
{
// Finalization code
}
// Nested class definition
private class aNestedClass
{
// class definition
}
}
Properties
Properties represent state in a class. However, properties are quite different from fields. Whereas a field stores data, a property provides a specific access point to that data. Properties extend the concept of the field by being able to restrict the incoming values and provide read-only and write-only data members.
using System;
public class Account
{
private double balance;
public double Balance
{
get
{
return balance;
}
set
{
if (value < 0)
throw
new ArgumentOutOfRangeException("value","Balance must
be greater than 0");
balance=value;
}
}
}
Static Type Members
The fields, properties, and methods we have seen so far in this chapter are accessible only with a reference to an instance of a class. However, not all functionality is best implemented tied to an instance of a class. .NET allows us to define type members that are callable directly from the class without requiring an instance of the class to be created. Fields, methods, and properties can all be declared in this fashion.
Static type members are useful if a particular action is not tied to an instance of the class, but is functionality that can be used as a standalone. For example, a Time class may define a GetCurrentTime() method that returns the current system clock time. Creating an instance of Time just to call GetCurrentTime() is not ideal since all classes would return the same value. Since this method is general to the Time class and is not specific to each instance, it is best implemented as a static member.
Static type members are commonplace with the .NET base class library. A trivial example of a static type member can be found with the System.DateTime class. System.DateTime.Now is a static function returning the system date, as seen in static_time_example.cs. Note that the DateTime class is used directly; we do not need to create an instance of DateTime to call the Now type member. The Now property within the DateTime class is declared as static:
using System;
public class static_time_example
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now);
}
}
Generally speaking, it is useful to think of static members as belonging to classes. Non-static, or instance members belong to objects, or instances of classes. If you'll recall, this is exactly how constants and static read-only fields perform. In fact, a constant is a special type of static field.
Remember, a field is a storage location for an object's data. A new storage location is created for each instance of an object. It is slightly different if a field is static. A static field denotes exactly one storage location. No matter how many instances of a class are created, there is only ever one copy of a static field.
Let's make one small change to our Account class to further explore how static fields affect our program. The following code is located in static_bankaccount.cs:
using System;
public class Account
{
private static double balance;
public double Balance
{
get
{
return balance;
}
set
{
balance=value;
}
}
}
class AtTheBank
{
[STAThread]
static void Main(string[] args)
{
Account mySavings= new Account();
Account myChecking=new Account();
mySavings.Balance=500;
Console.WriteLine("Savings balance: " + mySavings.Balance);
Console.WriteLine("Checking balance: " + myChecking.Balance);
}
}
The only change we've made to the Account class is in how we define the balance field:
private static double balance;
However, while this change is small, the corresponding effect on the program is quite noticeable. Look again at the two instances of Account, mySavings and myChecking:
Account mySavings= new Account();
Account myChecking=new Account();
mySavings.Balance=500;
Console.WriteLine("Savings balance: " + mySavings.Balance);
Console.WriteLine("Checking balance: " + myChecking.Balance);
If balance were an instance variable, then mySavings and myChecking would each have their own balance field. We would expect that the output of the above program would show that the mySavings instance had a balance of 500 while myChecking only had the default value of 0. However, balance is not an instance field. Rather, it is a static field. Because of this, the value of balance is shared across all instances of Account.
If a method is declared as static, then that method will perform an action on the class, not an instance of the class. A static member can only access and modify static fields. Let's modify the account one more time to show off the use of a static method. The following code is in static_bankaccount2.cs:
using System;
public class Account
{
private static double balance;
public double Balance
{
//...omitted for brevity
}
public static void addTen()
{
balance=balance+10;
}
}
class AtTheBank
{
[STAThread]
static void Main(string[] args)
{
Account mySavings= new Account();
Account myChecking=new Account();
mySavings.Balance=500;
Account.addTen();
Console.WriteLine("Savings balance: " + mySavings.Balance);
Console.WriteLine("Checking balance: " + myChecking.Balance);
}
}
Static methods act on the class, not the instances of the class. Static methods can access static fields, static properties, and other static methods. As such, you cannot call AddTen() through the mySavings or myChecking instances of the Account class. Rather, you must make the call directly against the Account class. However, as you can see from the output, the call does still affect the balance of every instance. This is because AddTen() adds to the static balance field. Here is the output of the above program:
C:\Class Design\Ch 02>static_bankaccount2.exe
Savings balance: 500
Checking balance: 500
Savings balance: 510
Checking balance: 510
Care must be taken when using static methods and fields. Only fields that truly must be shared across all instances should be marked as static. Similarly, only methods and properties that must act on static fields should be marked as static. An ideal situation to use static methods and fields is in conjunction with the read-only attribute.
Passing by Reference
Passing arguments by reference involves copying a reference to the data instead of the data itself. We think of this as if we are passing a pointer to the location in memory where the data is to the method. Both the client code and the method hold separate references to the same data. Because of this, changes made to the parameter by the method are visible to the client code. The changes a method makes to a parameter that can affect the client code are called side effects. When an argument is passed by reference, think of the parameter as an alias to the argument.
What actually happens is that the value is copied in to the method, just as it is when passed by value; when the method terminates, the value is copied back out again into the location from which it came, along with any changes that have been made to the value in the course of the method executing.
Passing an argument by reference requires the use of the additional ref keyword in both the method declaration, and the calling client code. Failure to use the ref keyword in the calling program results in a compile-time error:
Argument 'n': cannot convert from '
' to 'ref '.
The ref keyword overrides the default pass by value behavior. The following example demonstrates passing data by reference:
using System;
namespace parameter_types
{
public class Calculator
{
public static decimal CalculateInterest(ref decimal balance)
{
decimal interest = (decimal)45.11;
balance = balance + interest;
return (balance);
}
}
class BatchRun
{
static void Main()
{
decimal balance = (decimal)4566.54;
Console.WriteLine("Balance = " + balance);
Console.WriteLine("Balance + Interest = " +
Calculator.CalculateInterest(ref balance));
Console.WriteLine("Balance = " + balance);
}
}
}
Output Parameters
Output parameters solve the problem of getting a method to simulate the manipulation and passing back of more than one value. Similar to ref parameters, output parameters are also passed by reference and the out parameter also behaves like an alias to the argument, but there is one subtle and important difference to ref arguments, and that is that:
Important Arguments passed by reference using the out keyword do not have to be initialized before being passed to the method.
By implication, if an argument has no value (or if it is a null reference), then the method cannot inadvertently do any harm to the argument. Output parameters are also considered unassigned within a method; methods with out parameters must therefore initialize all out parameters before the method returns.
The following example parameter_types.cs demonstrates passing data by reference using out parameters:
using System;
public class Calculator
{
public static decimal CalculateInterest(out decimal balance)
{
decimal interest = (decimal)45.11;
balance = (decimal)4566.54;
balance += interest;
return (balance);
}
}
class BatchRun
{
static void Main()
{
decimal balance;
Console.WriteLine("Balance + Interest = " +
Calculator.CalculateInterest(out balance));
}
}
using System;
class MSILMethods
{
static void Main()
{
int result = 0;
int total;
int net = 0;
MethodExamples examples = new MethodExamples();
result = examples.ByValueMethod(7);
examples.ByRefMethod(ref result);
examples.OutputMethod(out total, result);
result = result + examples.OverloadMethod(total);
total = examples.OverloadMethod(result, total);
net = examples.ParamsMethod(result, total);
Console.WriteLine(net);
}
}
class MethodExamples
{
public int ByValueMethod(int a)
{
return a++;
}
public void ByRefMethod(ref int b)
{
b = b * 2;
}
public void OutputMethod(out int c, int d)
{
c = d / 4;
}
public int OverloadMethod(int e)
{
return e + 2;
}
public int OverloadMethod(int e, int f)
{
return e + f;
}
public int ParamsMethod(params int[] g)
{
int total = 0;
foreach(int num in g)
{
total = total + num;
}
return total + 1;
}
}