Non-primitive types are also called “Reference variables” or “Object References”. that is because they do not hold a these variables do not hold the value. They rather hold a reference to the address in the memory that holds the value. All the classes such as Array, String, ArrayList, StringBuilder etc. are Non-Primitive types.
Classes:
Classes are blueprints of our application and work as building blocks. We combine all the related fields, attributes, properties and functions in classes. To create a class:
public class Animal
{
}
In the above code public
is the access modifier. This modifier defines whether this class is visible or not.
In the following code we have a class with a field and a method (function)
public class Animal
{
public string AnimalName;
public void Information()
{
Console.WriteLine("This is a " + AnimalName);
}
}
Any function that has the keyword void
with it shows that this functions doesn’t return anything. If this function was to return a string
, we would have wrote something like this:
public string Information().
To create an object from a class and to use it’s fields and methods:
var dog = new Animal();
person.Name = "Jackie";
person.Information();
Static Modifier:
Static modifier tells us that a class can not be instantiated, we can not create objects of a static class. Also if we make a member of a class static, we can access it only through that class not through it’s objects as in the example above. If we want to access it we would have to:
var name = Animal.Name
Structure types:
In c# we have another type that is similar to classes which is called Structure or struct. Structure types are used when we need to create lightweight objects. You basically combine related fields and methods together. In terms of syntax, it is pretty similar to the class but instead of using the class keyword, we use struct.
struct HexColors
{
public int Red;
public int Blue;
public int Yellow;
}
Array:
Array is another non-primitive types in C#, so what is an array? Array is a collection of variables of the same type. Let’s say you were to work with 4 different numbers as below:
int number1;
int number2;
int number3;
int number4;
So instead of creating these 4 numbers we can create one array of int type as below:
int[] numbers = new int[4];
First square bracket is to tell the compiler that we are declaring an array, second one is to set the size of the that array. In c# arrays have fix size and we have to specify during declaring it can not be changed.
Why use the keyword new?
In c# array is a class and when you are declaring an array, we are creating instance/object of array class.
To access the elements of an array we do it by giving the index of the array in square bracket, like code below.
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
In c# arrays are zero indexed which means that first element of the array has zero index. If you know the the value you will be storing in an array, you can use object initialization syntax and make this code shorter, see code below:
int[] numbers = new int[4] { 1, 2, 3};
String:
String is a sequence of characters e.g. “Hello World”. To declare string:
string greetings = "Hello";
We can also concataenate strings together using `+` keyword.
string firstName = "Kashif";
string lastName = "Mahmood";
string name = firstName + " " + lastName;
In the above code it could become hard to understand what the output would look like, We can make it look better using Format method of string class.
string firstName = "Kashif";
string lastName = "Mahmood";
string name = string.Format("{0} {1}", firstName, lastName)
Escape Characters:
In c# there are some special characters which are called escape characters.
Escape Character
Description
\n
Adds new line to a string
\t
Adds tab to string
\\
The `\` character itself
\'
Single quote character
\"
Double quote character.
Since backslash character is used to prefix escape characters, when we have to use backslash character itself we need to prefix it with another backslash characters. This makes if we have to have a path to a file in a string we have to:
string path = "c:\\folder1\\file.txt";
Verbatim Strings:
Some time the strings has a lot of escape characters such as the following one:
string information = " Hello Kashif\n look down\n c:\\folder1\\file.txt";
We can write the same string using a verbatim string, all you have to do add an @ sign and we don’t have to use escape characters anymore.
string information = @"Hello Kashif
look down
c:\folder1\file.txt";
Enums:
Sometime we have the pair our data in name and value pairs, and has to define multiple related constants. For that we use Enums. Below is how you declare enums:
public enum SubscriptionTypes
{
Basic = 1,
Silver = 2,
Gold = 3,
}
Enums by default are int type but you can make it byte in declaration.
public enum SubscriptionTypes : byte
{
}