C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Introduction
So far, we have seen
operators in C#, and let's see the usage of char's and strings in
C# programming.
Objective
The main objective of this article is to learn about character's and strings in
C# programming.
Char
'char
' is the keyword used to represent a Unicode character. .Net Framework type of char
is System.Char. It is a 16-bit numeric value (means it occupies 2 bytes).
char
literal is specified in single quotes.
Ex:
Escape Sequences:
Escape sequences are the characters that cannot be interpreted or expressed literally.
Let's have a simple code that explains about char
literals in detail,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
char s = 'S'; // Simple Character
char nextLine = '\n'; // Escape Sequence for going to the next line
char back = '\r'; // Escape Sequence for carriage return
Console.WriteLine(nextLine); // Prints nothing but skips to the next line.
Console.WriteLine(s); // Prints the character 'S'
Console.WriteLine(back);
}
}
}
In the above code, we are using different types of characters used in C#.
Note: please read comments in the code.
Now press Ctrl + F5 and see the following output in your Console,
Below table shows the list of Escape sequence characters.
Char | Description | Value |
\' | Single quote | 0x0027 |
\" | Double quote | 0x0022 |
\\ | Backslash | 0x005C |
\0 | Null | 0x0000 |
\a | Alert | 0x0007 |
\b | Backspace | 0x0008 |
\f | Form feed | 0x000C |
\n | New line | 0x000A |
\r | Carriage return | 0x000D |
\t | Horizontal tab | 0x0009 |
\v | Vertical tab | 0x000B |
Conversions
char can be converted implicitly to int
, uint
, long
, ulong
, ushort
, float
, double
or decimal
. And there are no implicit conversions from other types (explicit conversion is required).
System.Char consists of several methods that works with char values.
String
- string is an immutable object which represents a series of Unicode characters.
- It can be used in many methods without its data change.
- It is a valid which reduces copies.
- It makes the programs more robust.
- .Net Framework type is 'System.String'.
string literal is specified in the double quotes.
Ex:
What is Immutability?
'String' is the class which provides so many methods creating, comparing and manipulating the strings
.
Let's have a simple code that explains strings well,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
string s1 = "Hello";
string s2 = "Hello";
Console.WriteLine(s1 == s2); // Prints True as it follows the Valu-type approach
string s3 = "Escape Sequence:\n";
Console.WriteLine(s3); // Prints Escape Sequence and goes to the next line
}
}
}
In the above code, we are comparing strings and also used escape sequences.
Note: please read comments in the code.
The output of the above code will be,
String Concatenation:
To concatenate (combine) two strings, we have to use '+
' operator.
Ex:
string s = "Abhi" + "ram" // result becomes Abhiram
We can also add a non-string value like,
String s = "a" + 6 // a6
String Comparisions:
string doesn't supports <
, >
operators for comparisons. We have to use CompareTo method to compare the strings.
Conclusion
In this article, we have discussed Characters and Strings in C# with examples. Hope you understand.
Thanks for reading.
Regards,
Krishna.
.