Type Casting
Notes:
I. C# Data type Conversion : [Starts at: 00min:18sec]
II. C# Parse & ToString methods : [Starts at: 28min:51sec]
III. C# Convert class : [Starts at: 39min:09sec]
I. C# Data type Conversion : [Starts at: 00min:18sec]
Changing the type of data from one datatype to another.
Ex:
changing integer data to float storing it in a float variable,
changing any primitive data to string type.
Storing derived class object reference in to base class object reference etc.
There are two types of conversions:
1. c# Implicit type conversion ( Coercion ):
is a type of conversion, automatically performed by the compiler implicitly.
Based on the size of data type, when we assign a variable of smaller capacity to a variable of larger
capacity, compiler will implicitly converts and stores the data.
Example:
short s =10; // short is 2 byte
int i; // int is of 4 byte
i=s; // implicit type conversion
2. c# Explicit type conversion ( Type casting ):
is a type conversion, which is explicitly defined by the programmer instead of being performed by the compiler.
When a larger data type variable value is converted and stored in smaller data type variable, then we need
to explicitly specify the data type to which the value need to be converted.
Example:
int i = 10;
short s;
s= (short) i; // explicit type conversion
Example Code 1:
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
char ch = 'A'; // char is 2 bytes
byte byte1 = (byte) ch; // 2 bytes to 1 byte needs explicit type cast
Debug.Log(byte1); // 65
short s1 = 32100; // short is 2 bytes
int i1 = s1; // 2 bytes to 4 bytes implicitly type casts
Debug.Log (i1); // 32100
int i2 = 35000;
short s2 = (short)i2; //4 bytes to 2 bytes loss of data
Debug.Log(s2); // some garbage number
int i3 = 22000;
float f1 = i3; // 4 bytes to 4 bytes implicitly type casts
Debug.Log (f1); // 22000
float f2 = 3.142f;
int i4 = (int)f2; // float to int losss of precision
Debug.Log(i4); //3
double d1 = 3.44444444d;
float f3 = (float)d1; // double to float loss of precision
Debug.Log (f3); // 3.444444
long l1 = 12345678987654321L; // for long numbers
Debug.Log (l1); // 12345678987654321
v
}
}
II. C# Parse & ToString methods : [Starts at: 28min:51sec]
datatype dataype.Parse(string) :
Converts given string to equivalent datatype value and return it
bool dataype.TryParse(string)
Tries to convert given string to equivalent datatype value and returns true or false
string instance.ToString()
Converts given instance value to string and returns it
Example code 2:
using UnityEngine;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
// string to primitive
int num = int.Parse("22");
Debug.Log (num);
float num2 = float.Parse ("22.22");
Debug.Log (num2);
// primitive to string
int num3 = 33;
string num3String = num3.ToString ();
Debug.Log (num3String);
}
}
III. C# Convert class : [Starts at: 39min:09sec]
Example Code 3:
using UnityEngine;
// Convert class is in System namespace
using System;
public class FirstScript : MonoBehaviour {
// Use this for initialization
void Start ()
{
int num = Convert.ToInt32 ("22");
Debug.Log (num); // 22
}
}