DotNet W
DotNet W
DotNet W
Value types
Reference types
Pointer types
Value Type
Value type variables can be assigned a value directly. They are derived from the
class System.ValueType.
The value types directly contain data. Some examples are int, char, and float, which stores
numbers, alphabets, and floating point numbers, respectively. When you declare an int type,
the system allocates memory to store the value.
Default
Type Represents Range
Value
64-bit double-precision
double (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D
floating point type
32-bit single-precision
float -3.4 x 1038 to + 3.4 x 1038 0.0F
floating point type
-9,223,372,036,854,775,808 to
long 64-bit signed integer type 0L
9,223,372,036,854,775,807
using System;
namespace DataTypeApplication {
class Program {
static void Main(string[] args) {
Console.WriteLine("Size of int: {0}", sizeof(int));
Console.ReadLine();
}
}
}
Reference Type
The reference types do not contain the actual data stored in a variable, but they contain a
reference to the variables.
In other words, they refer to a memory location. Using multiple variables, the reference types
can refer to a memory location. If the data in the memory location is changed by one of the
variables, the other variable automatically reflects this change in value. Example of built-
in reference types are: object, dynamic, and string.
Object 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.
When a value type is converted to object type, it is called boxing and on the other hand, when
an object type is converted to a value type, it is called unboxing.
object obj;
obj = 100; // this is boxing
Dynamic Type
You can store any type of value in the dynamic data type variable. Type checking for these
types of variables takes place at run-time.
For example,
dynamic d = 20;
Dynamic types are similar to object types except that type checking for object type variables
takes place at compile time, whereas that for the dynamic type variables takes place at run
time.
String Type
The String Type allows you to assign any string values to a variable. The string type is an
alias for the System.String class. It is derived from object type. The value for a string type
can be assigned using string literals in two forms: quoted and @quoted.
For example,
@"Tutorials Point";
Pointer Type
Pointer type variables store the memory address of another type. Pointers in C# have the
same capabilities as the pointers in C or C++.
type* identifier;
For example,
char* cptr;
int* iptr;
Data Types:
Value Types: Includes basic types such as integers, floating-point numbers, booleans,
and structures.
o Examples: int, double, bool, struct
Reference Types: Refers to objects that store references to the actual data.
o Examples: string, arrays, class, interface
Nullable Types: Allows value types to represent undefined or missing values.
o Example: int?, double?
Enumerations: Defines a set of named constants.
o Example: enum Day { Sunday, Monday, ... }
Tuples: Groups multiple values of different types into a single object.
o Example: Tuple<int, string>
Anonymous Types: Provides a way to encapsulate a set of read-only properties into a
single object without having to define a type.
o Example: var person = new { Name = "John", Age = 30 };
Creating a String Object
You can create string object using one of the following methods −
using System;
namespace StringApplication {
class Program {
When the above code is compiled and executed, it produces the following result −
Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
Chars
1
Gets the Char object at a specified position in the current String object.
Length
2
Gets the number of characters in the current String object.
Comparing Strings
using System;
namespace StringApplication {
class StringProg {
if (String.Compare(str1, str2) == 0) {
Console.WriteLine(str1 + " and " + str2 + " are equal.");
} else {
Console.WriteLine(str1 + " and " + str2 + " are not equal.");
}
Console.ReadKey() ;
}
}
}
When the above code is compiled and executed, it produces the following result −
class StringProg {
if (str.Contains("test")) {
Console.WriteLine("The sequence 'test' was found.");
}
Console.ReadKey() ;
}
}
}
When the above code is compiled and executed, it produces the following result −
namespace StringApplication {
class StringProg {
When the above code is compiled and executed, it produces the following result −
San Pedro
Joining Strings
using System;
namespace StringApplication {
class StringProg {
When the above code is compiled and executed, it produces the following result −
Syntax
if(boolean_expression) {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}
If the boolean expression evaluates to true, then the if block of code is executed,
otherwise else block of code is executed.
AD
using System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 100;
When the above code is compiled and executed, it produces the following result −
An if statement can be followed by an optional else if...else statement, which is very useful to
test various conditions using single if...else if statement.
When using if, else if, else statements there are few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.
Syntax
using System;
namespace DecisionMaking {
class Program {
static void Main(string[] args) {
/* local variable definition */
int a = 100;
When the above code is compiled and executed, it produces the following result −