char, string Data Types in C#.NET
char & string Data Types
A char data type (alias for System.Char) is used to represent
a single Unicode character. To represent more than one char, you use a string,
which is basically just a list of chars
Example:
char ch =
'H';
string helloWorld = "Hello, world!";
foreach(char
c in helloWorld)
{
Console.WriteLine(c);
}
A string is a piece of text. It's usually consists of 2
characters or more, since if it's just one, you should consider using a char
instead. However,
strings can be empty as well or even null, since it's a
reference type. A string can be declared much like the other data types we have
worked with already.
Example:
string
strTitle = “Hello World;
strTitle = strTitle + “, How are
you”;
strings are immutable.
In C#, strings are immutable, which basically means that once
they are created, they can't be changed. That's obviously not very practical in
daily use.
so, the framework helps us - instead of having to keep declaring new strings to make changes, it simply creates a new string for you each time you change the existing one.
Example:
string
numbers = "";
for (int i =
0; i < 100; i++)
{
numbers += i.ToString();
}
Console.WriteLine(numbers);
In this case, we loop 100 times, each time appending the
current index to the string. With the knowledge you just gained, you now know
that instead of altering.
the existing string to include the latest number, a new
string is created and then assigned to the old variable, leaving the old value
to be cleaned up by the framework.
And this happens 100 times! Instead, it's generally
recommended to use a so-called String Builder.
In C#, a StringBuilder
is a mutable series of characters that can be extended to store more
characters if required. Unlike with strings, modifying a StringBuilder instance
does not result in the creation of a new instance in memory.
StringBuilder
numbers = new StringBuilder();
for
(int i = 0; i < 10000; i++)
numbers.Append(i);
Console.WriteLine(numbers.ToString());
Basic string operations
v concatenate
v Length
v indexOf
v Substring()
v Replace()
v Contains()
v StartsWith()
v EndsWith()
Value Types Vs Reference
Types
The difference between
them is very simple—a variable of a value type directly contains
data, while a variable of a reference type just stores a reference to data, as
shown as follows:
As you can see, a Value type stores its actual Value directly in the Stack memory, while a Reference type only stores a Reference here. The actual value is in the Heap memory. Therefore, it is also possible to have two or more variables of a reference type that reference exactly the same value.
Value Types are typically created at compile time and stored
in stack memory. This allocation on the stack provides efficient
memory management and quick access to the values. However, it's important to
highlight that the stack memory is not accessible by the garbage collector.
Unlike heap memory, which is managed by the garbage collector to reclaim unused
objects, the stack memory is not subject to garbage collection. This means that
Value Types have a limited lifetime and are automatically deallocated once they
go out of scope.
Reference Type variables, such as Classes, Objects, Arrays,
Indexers, and Interfaces, are stored in a distinct area of memory called the
heap. This memory region allows for flexible allocation and
deallocation of objects during runtime. When a reference type variable
is no longer in use, it can be marked for garbage collection. The garbage
collector periodically identifies, and releases memory occupied by unreferenced
objects, ensuring efficient memory management within the heap.
Convert()
Convert class provides different methods to convert a base
data type to another base data type. The base types supported by the Convert
class are Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32,
UInt64, Single, Double, Decimal, DateTime, and String
Example:
int
a;
Console.WriteLine("Enter a value:
");
a
= Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Entered
value is : {0});
Object Data Type
The Object Type is the ultimate base class for all data types
in C# Common Type System (CTS). Object is an alias for System.Object class. The
object types can be assigned values of any other types, value types, reference
types, predefined or user-defined types. However, before assigning values, it
needs type conversion.
Example:
int
i=10;
object o = i; //boxing
int
j = (int)o; //unboxing
Boxing vs UnBoxing
Boxing is
the process of converting a value type to
the type object or to any interface type implemented by
this value type. When the common language runtime (CLR) boxes a value type, it
wraps the value inside a System.Object instance
and stores it on the managed heap.
Unboxing
extracts the value type from the object. Boxing is implicit; unboxing is
explicit. The concept of boxing and unboxing underlies the C# unified
view of the type of system in which a value of any type can be treated as an
object.
static void
Main(string[] args)
{
int i = 100;
Object o = i;
string name = "Hi, Welcome to
Boxing and Unboxing Demo";
Object o1 = name;
string name1;
name1 = (string)o1;
int j;
j = (int)o;
Console.WriteLine(o);
Console.WriteLine(i);
Console.WriteLine(j);
Console.WriteLine(name1);
Console.ReadLine();
}
C# program to swap 2 numbers and then
print
using System;
namespace SwapNumbers
{
class
Swapping
{
static void Main(string[] args)
{
int
a, b, temp;
Console.WriteLine("Enter
a value:");
a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter
b value:");
b = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Entered values a = {0} and b = {1}", a,b); temp = a;
a
= b;
b = temp; Console.WriteLine("After Swapping values a = {0} and b = {1}",a,b);
Console.ReadLine();
}}}
GUID Data Type
A Globally Unique Identifier or GUID represents a
gigantic identification number with memory size 16 bytes(128-bit). This data type is used for the global
identification of objects, programs, records, and so on. The important property
of a GUID is that each value is globally unique. The value is generated
by an algorithm developed by Microsoft.
Guid is present in System namespace in C#
Example:
Guid
demoGuid = Guid.NewGuid();
Console.WriteLine(demoGuid.ToString());
O/P
7d8af258-07ed-46bd-8188-4513550932c2
Comments
Post a Comment