C# defines 3 other value types called enum, struct and nullable types. I will define them and give relevant examples here. In the previous article, I explained about the simple types.
You can try these programs in the browser here. Have fun learning.
Table of Contents
Enum type
enum -Declares a set of constant types.
enum coord
{
x,
y=20,
z
}
static void EnumCall()
{
Console.WriteLine((int) coord.x);// prints 0
Console.WriteLine((int)coord.y);//prints 20
Console.WriteLine((int )coord.z);//prints 21
}
The values are initialized by default when there is nothing specified. X is 0, y is 20 and Z is 21. The constants after y will be incremented by the value of 1 with the previous constant. For instance, y had not been initialized then x will be 0, y is 1 and Z is 2. EnumCall() function can be called from any place that has the context of the enum known.
Structure type
A struct type can encapsulate data and methods similar to a class. ‘struct’ keyword is used.
The structure provides a default constructor and will not allow the user to declare a parameter-less constructor. We cannot inherit from a structure in C#.
A structure created without using the new operator will not call the constructor and the members can be uninitialized.
using System;
struct Person
{
public int age;
public string county;
public string name;
private string yearofbirth;
public string BirthYear
{
get
{
return yearofbirth;
}
set
{
yearofbirth = value;
}
}
public Person(int age, string place, string nme, string dob)
{
this.age = age;
this.county = place;
this.name = nme;
this.yearofbirth = dob;
}
}
namespace TelestreakTutorial
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Person p = new Person(20, "Chicago", "Joshua" ,"2001");
}
}
}
An example of how we create a structure is shown. The structure can use the ‘new’ operator to invoke the struct constructor. Henceforth, the line contains the object name and the values for all the members of the structure. We can see the members of the structure are public and private. I have added a public member by the name Birthyear which can be used to update the ‘yearofbirth’ which is a private member. One cannot directly modify the private members of the structure. In C#, we use the public members which give us access to these private members. For instance, I used the ‘BirthYear’ member with get and set methods. We can use these methods to call events and update other variables which are dependent on the yearofbirth. Although yearofbirth cannot change, this is just an example 🙂
On a struct we cannot use abstract, virtual, sealed and protected keywords.
Nullable type
We can see this as a null value which is of a value type. For instance, a bool value can have true or false or null. Nullable type belongs to a generic structure for System.Nullable<T>.
When we have a variable whose value is known or set or something may not have any value at all. We use null type in such cases. We can initialize a value by adding a ? to the type as shown in the example.
int? max = 65535;
bool? isValid = null;
double?[] dArr = new double?[20];
if(dArr[0].HasValue )
{
Console.WriteLine("Contains value in dArr");
}
else
{
Console.WriteLine("No value in dArr");
}
if(max.HasValue)
{
Console.WriteLine("Yes, Max has a value");
Console.WriteLine(max.Value);
}
else
{
Console.WriteLine("No value is present in max :( ");
}
//**************
//Output
No value in dArr
Yes, Max has a value
65535
In the example, I have 3 types which have ? which indicates that max value will be null if it does not have 65535. Same will happen with bool. dArr is the variable used to declare an array of 20 items which are of type null and type is of Nullable value type.
Nullable type has 2 properties which I can query on.
- Nullable<T>.HasValue: This queries if there exist a value which is not a null type.
- Nullable<T>.Value : Get the value if HasValue return true. If it does not have a value then, this query will throw an exception which indicates an invalid operation.
In the example above, I demonstrated how we can use these two functions to access the values.
Conclusion
We learnt about enum, structure and nullable type. I recommend you try out the examples and read through the concepts to gain a firm understanding. We will delve into reference types in the next tutorial.
Like our content? Subscribe here for books and interview tips for C#.
Prev : Variables and Simple Value types
Next: Coming soon