Why do we see "<" and ">" in Unity C# code?
The < and > symbols are used in many circumstances in Unity C# code. These symbols serve various functions and are a part of the syntax of the C# language.
The following are some typical situations in which these symbols can be seen:
Inequality or comparison operator
Generics
Inequality or comparison operator
The inequality or comparison operators < and > are used to check if a value is less than or greater than another value, respectively. For example:
using System;class Program{static void Main(){int a = 4;int b = 12;if (a < b){// Code inside this block will be executed because 'a' is less than 'b'.Console.WriteLine("The condition is true.");}}}
Code explanation
The code above demonstrates a simple program that compares two integers (a and b) using if statement and inequality operator <, and prints a message to the console based on the comparison result.
Bitwise operators
Angle brackets can be used in bitwise shift operations in certain situations. For example:
using System;class Program{static void Main(){int x = 8;int y = x << 2; // Left shift x by 2 bits// 'y' will be 32 (binary: 100000)Console.WriteLine("The value of 'y' after left-shifting 'x' by 2 bits: " + y);}}
Code explanation
In the code above, an integer x is assigned the value 8, another integer y is assigned the result of left-shifting x by 2 bits (x << 2). A message is printed to the console indicating that the value of y after left-shifting x by 2 bits is 32, with a binary representation of 100000.
Generics
We can define classes, collections, and methods in C# with placeholders for the data types they handle, thanks to generics. The < and > symbols are used to specify the type parameters.
Generic class
We are creating the generic class in the example below:
using System;public class MyClass<T>{private T data;public MyClass(T initialData){this.data = initialData;}public void DisplayData(){Console.WriteLine($"Data: {data}");}}class Program{static void Main(){// Instantiate MyClass with int as the type parameterMyClass<int> intInstance = new MyClass<int>(30);intInstance.DisplayData();// Instantiate MyClass with string as the type parameterMyClass<string> stringInstance = new MyClass<string>("Hello, World!");stringInstance.DisplayData();}}
Code explanation
The provided C# code is a generic class (MyClass<T>) example with a simple demonstration in the Main method. It creates instances of MyClass with different generic types (int and string) and displays the data stored in each instance.
Generic method
Now, we are creating the generic method as in the example below:
using System;public class Example{public T MyMethod<T>(T parameter){// Method implementation with a placeholder type Treturn parameter;}static void Main(){Example example = new Example();// Call MyMethod with int as the type parameterint resultInt = example.MyMethod(25);Console.WriteLine($"Result with int: {resultInt}");// Call MyMethod with string as the type parameterstring resultString = example.MyMethod("Hello, World!");Console.WriteLine($"Result with string: {resultString}");}}
Code explanation
In the example above, a generic method named MyMethod is defined. The method takes a parameter of Main method, an instance of Example class is created, and MyMethod is called twice with different types (int and string). The results are then printed to the console.
Collections
Unity uses generic collections extensively, such as List<T>, where <T> indicates the type of elements stored in the list.
using System;using System.Collections.Generic;class Program{static void Main(){// Create a List<int> and add some integersList<int> integerList = new List<int>();integerList.Add(5);integerList.Add(10);integerList.Add(15);// Print the elements of the integer listConsole.WriteLine("Integer List:");foreach (int num in integerList){Console.WriteLine(num);}// Create a List<string> and add some stringsList<string> stringList = new List<string>();stringList.Add("Apple");stringList.Add("Mango");stringList.Add("Cherry");// Print the elements of the string listConsole.WriteLine("\nString List:");foreach (string fruit in stringList){Console.WriteLine(fruit);}}}
Code explanation
In the code above, the Main method creates two lists: integerList and stringList. Integers are added to integerList, and strings are added to stringList. The elements of both lists are then printed to the console using a foreach loop.
Conclusion
To summarize the concept discussed above, the < and > symbols are integral to Unity C# syntax, serving various purposes such as comparison, bitwise operations, and defining generics. Whether utilized in inequality comparisons, bitwise shifts, or specifying type parameters, they define code behavior and structure crucial in Unity projects. Moreover, their usage extends to generic collections like List, which is essential for efficient data management. Understanding their versatility is fundamental for proficient Unity programming.
Free Resources