Introduction to C# Literals
Literals in C# are the fixed value used by a variable that is predefined and cannot be modified during the execution of the code. These are the convenient form of constant values like other variables but their values cannot be changed. The value used by a variable can be integer, decimal, floating type or string. There are different types of literals in C# with different forms. There are various types of literals in C#.
- Integer Literals
- String Literals
- Character Literals
- Floating-point Literals
- Boolean Literals
Top 5 Types of Literals in C#
Following are the different types of literals in C#.
1. Integer Literals
The literal of integer type can be octal, decimal or hexadecimal. The prefix is used to specify whether it is decimal, octal or hexadecimal. U and u are also used as a suffix with integer type literals for unsigned numbers and l and L used for long numbers. Every literal is of integer type by default.
- Decimal Literals: In the decimal type of literals 0-9 digits are allowed. No prefix is required for the decimal type of literals.
int x = 100; // decimal type
- Octal Literals: In the octal type of literals 0-7 digits are allowed. 0 is used as a prefix to specify the form of octal type literals.
int x = 072; // octal type
- Hexa-decimal literals: In the hexadecimal type of literals, digits from 0- 9 and characters from A-f are allowed. Uppercase and lowercase both types of characters are allowed in this case. 0X or 0x is used as a prefix to specify the form of hexadecimal type of literals.
int x = 0x123f; // hexadecimal type
2. String Literals
The string type literals are enclosed in (“”)/ double quotes and also can be started with @””. Long lines can be broken into multiple lines with string literals and be separated by using blank spaces.
string s= "Hi"; // string literals
3. Character Literals
The character type literals are enclosed in (‘’)/single quotes. There are three ways to specify character literals.
- Single Quote: Characters literals can be specified as single char using a single quote.
- Unicode Representation: Character literals can be specified using Unicode Representation ‘\uxxxx’ where xxxx are the hexadecimal numbers.
- Escape Sequence: There are some escape characters known as char literals.
char c = '\n';
Following are some escape sequence literals explained with there meanings.
Escape Sequence | Meaning |
\\ | Character |
\’ | Character |
\’’ | Character |
\? | Character |
\a | Alert |
\b | Backspace |
\n | Newline |
\f | Form feed |
\v | Vertical tab |
\xhh | Hexadecimal number |
4. Floating Point Literals
In the floating type of literal, there is an integer part, a fractional part, decimal part and exponent part. The floating type literal is of double type. F or F can be used as a suffix to specify the value as it cannot be assigned directly to the float variable.
5. Boolean Literals
In the Boolean type of literals, true and false will be the only two values.
Examples of C# Literals
Below are the examples which show how we can implement all the above literals in C#
Example #1 – Integer Literal
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Literals
{
class Program
{
static void Main(string[] args)
{
int x = 212; // decimal literal
int y = 0145; // octal literal
int z = 0x4b; // hexadecimal literal
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);
Console.ReadLine();
}
}
}
Output:
Explanation: In the above example, there are various forms of integer type literals. In this no prefix is used for decimal form, 0 is used to specify the octal form and 0x is used for specifying the hexadecimal number. Using prefix we can define the form of integer type literal. In this code, first, there is a literal of decimal type with no prefix, a second type is an octal form with 0 as prefix and at last, we have a hexadecimal type with 0x as a prefix.
Example #2 – Floating Point Literal
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Literals
{
class Program
{
static void Main(string[] args)
{
double x = 187.231;
double y = 0141.361;
double z = 374159E-4F;
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(z);
Console.ReadLine();
}
}
}
Output:
Explanation: In the above example, floating-point literals are implemented. It can be a decimal number, fractional or any exponent. So we can represent it either in decimal or in exponential form. The floating type literal is of double type. F or F can be used as a suffix to specify the value as it cannot be assigned directly to the float variable.
Example #3 – Character Literals
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Literals
{
class Program
{
static void Main(string[] args)
{
char c = 'b';
char ch = '\u0071';
Console.WriteLine(c);
Console.WriteLine(ch);
Console.WriteLine("\nHello World\t!");
Console.ReadLine();
}
}
}
Output:
Explanation: In the above example, character type literals are implemented. In the above code, all three forms of character type are shown. We can specify the character literal using a single quote, Unicode representation, and escape sequence. We have multiple types of escape characters with their own meanings. In this code, the first single quote character is specified where the second one has Unicode representation and then, at last, we have escape form type of character literals.
Example #4 – String Literal
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Literals
{
class Program
{
static void Main(string[] args)
{
String s1 = "This is C# programming";
String s2 = @"This is C# programming";
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.ReadLine();
}
}
}
Output:
Explanation: In the above example, string literals are implemented. There are two ways to specify string literals as shown in the code. First, it is implemented using double quotes and then @ symbol is used to specify the string.
Example #5 – Boolean Type Literal
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Literals
{
class Program
{
static void Main(string[] args)
{
bool x = true;
bool y = false;
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadLine();
}
}
}
Output:
Explanation: In the above example, Boolean type literals are implemented which has two value either true or false.
Conclusion
So literals are the fixed values. In C# there are different types of literals with specific form types. It can be of integer, Boolean, string, character type of literals.
Recommended Articles
This is a guide to C# Literals. Here we discuss the basic concept and top 5 types of C# Literals along with different examples and code implementation. You may also look at the following articles to learn more –
6 Online Courses | 18 Hands-on Project | 90+ Hours | Verifiable Certificate of Completion
4.6
View Course
Related Courses