Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

C# Chapter 3

Q. What are various data types supported by C#? illustrate the use of each through suitable examples.

Ans.  Data types in a programming language describes that what type of data a variable can hold. C# is a strongly typed language, therefore every variable and object must have a declared type before using. When we declare a variable, we have to tell the compiler about what type of the data the variable can hold or which data type the variable belongs to.
 In C# there are three categories of Data Types :-
·         Value Type – The values types hold actual values.
·         Reference Type – Hold references to values stored somewhere in memory.

·         Pointer Type- Pointer manipulation (unsafe Code)

As we know Data is physically stored inside cells of memory. This memory could be physical memory (Hard disk) or logical memory (RAM). Any cell of memory is represented with a unique address. We must have to know before the discussion of the data types. This is about variables and constants. A Variable is a named cell of memory used for data storage. A Variable value can be changed anytime. Every variable must have a type and this type must be set before it is used. Qualifying a variable with a type is called as declaration of variable. All variable can be divided into seven categories as per there use:-
  1. Static variables
  2. Variable of instance
  3. Array's elements
  4. Parameters given by reference
  5. Parameters given by value
  6. Returned values
  7. Local variables.
Static Variables will be alive throughout the life of a program. It can be declared using static modifier.
An Instance variable is a variable declared without static modifier. Usually it is declared inside a class or structure definition.
 Parameter Values can be viewed as input parameters into methods/functions.

public static void Sum(int a, int b)
{
   Console.WriteLine("The sum of elements {0} and {1} is {2}",a,b,a + b);
}

CSharp has something called Reference variables. Reference variables also have a modifier out which can be used before their type.

Constants in C#:

Constant type of data cannot be changed at the time of program execution. To declare a constant variable the keyword const is used. An example for the constant declaration is:
e.g.
const double PI = 3.1415;
sbyte: Holds 8-bit signed integers. The s in sbyte stands for signed, meaning that the variable's value can be either positive or negative. The smallest possible value for sbyte variable is -128; the largest possible value is 127.
byte: Holds 8-bit unsigned integers. Unlike sbyte variables, byte variables are not signed and can only hold positive numbers. The smallest possible value for a byte variable is 0; the largest possible value is 255.
short: Holds 16-bit signed integers. The smallest possible value for a short variable is -32,768; the largest possible value is 32,767.
ushort: Holds 16-bit unsigned integers. The u in ushort stands for unsigned. The smallest possible value of an ushort variable is 0; the largest possible value is 65,535.
int: Holds 32-bit signed integers. The smallest possible value of an int variable is -2,147,483,648; the largest possible value is 2,147,483,647.
uint: Holds 32-bit unsigned integers. The u in uint stands for unsigned. The smallest possible value of a uint variable is 0; the largest possible value is 4,294,967,295.
long: Holds 64-bit signed integers. The smallest possible value of a long variable is 9,223,372,036,854,775,808; the largest possible value is 9,223,372,036,854,775,807.
ulong: Holds 64-bit unsigned integers. The u in ulong stands for unsigned. The smallest possible value of a ulong variable is 0; the largest possible value is 18,446,744,073,709,551,615.
char: Holds 16-bit Unicode characters. The smallest possible value of a char variable is the Unicode character whose value is 0; the largest possible value is the Unicode character whose value is 65,535.
float: Holds a 32-bit signed floating-point value. The smallest possible value of a float type is approximately 1.5 times 10 to the 45th power; the largest possible value is approximately 3.4 times 10 to the 38th power.
double: Holds a 64-bit signed floating-point value. The smallest possible value of a double is approximately 5 times 10 to the 324th; the largest possible value is approximately 1.7 times 10 to the 308th.
decimal: Holds a 128-bit signed floating-point value. Variables of type decimal are good for financial calculations. The smallest possible value of a decimal type is approximately 1 times 10 to the 28th power; the largest possible value is approximately 7.9 times 10 to the 28th power.
bool: Holds one of two possible values, true or false. The use of the bool type is one of the areas in which C# breaks from its C and C++ heritage. In C and C++, the integer value 0 was synonymous with false, and any nonzero value was synonymous with true. In C#, however, the types are not synonymous. You cannot convert an integer variable into an equivalent bool value. If you want to work with a variable that needs to represent a true or false condition, use a bool variable and not an int variable.
Predefined C# reference types
string: Represents a string of Unicode characters. It allows easy manipulation and assignment of strings. Strings are immutable, meaning that once it is created it can't be modified. So when you try to modify a string, such as concatenating it with another string, a new string object is actually created to hold the new resulting string.
object: Represents a general purpose type. In C#, all predefined and user-defined types inherit from the object type or System.Object class.

No comments:

Post a Comment