Searching...

Translate

Some Output Options

v Some Output Options

   When data has been output using a WriteLine( ) statement, it has been displayed using the default format. However, the .NET Framework defines a sophisticated formatting mechanism that gives you detailed control over how data is displayed. Using these options, you will be able to specify the way values look when output via a WriteLine( ) statement.

Ø Different Formats of WriteLine statement

§  I/P : -   Console.WriteLine("February has {0} or {1} days.", 28, 29);
O/P: -   February has 28 or 29 days.

§  I/P : -   Console.WriteLine("February has {0,10} or {1,5} days.", 28, 29);
O/P: -   February has 28 or 29 days.

§  I/P : -   Console.WriteLine("Here is 10/3: {0:#.##}", 10.0/3.0);
O/P: -   Here is 10/3: 3.33

§  I/P : -   Console.WriteLine("{0:###,###.##}", 123456.56);
O/P: -   123,456.56

§  I/P : -   balance = 12323.09m;
Console.WriteLine("Current balance is {0:C}", balance);
O/P: -   Current balance is $12,323.09

v Literals
   In C#, literals refer to fixed values that are represented in their human-readable form.
v Constants

   “A constant is a variable whose value cannot be changed throughout its lifetime”.

   Prefixing a variable with the const keyword when it is declared and initialized designates that variable as a constant:

const int a = 100; // this value cannot be changed.

   Constants have the following characteristics:

1.     They must be initialized when they are declared, and once a value has been assigned, it can never be overwritten.
2.     Constants are always implicitly static. However, notice that you don ’ t have to (and, in fact, are not permitted to) include the static modifier in the constant declaration.


v The Scope and Lifetime of Variables

§  Scope: - The scope of a variable is the region of code from which the variable can be accessed.

   Scope depends on the type of the variable and place of its declaration.

   Each time you start a new block, you are creating a new scope. A scope determines what names are visible to other parts of your program without qualification. It also determines the lifetime of local variables.

   A local variable is in scope until a closing brace indicates the end of the block statement or method in which it was declared.

   Consider the following code snippet:








using System;

class ScopeTest
{
static int m= 20;
Console.WriteLine(j);
public static void Main()
{
int n = 30;
Console.WriteLine(j);
void function(int x,int y)
{
            int a;
                                    }
}
}

   Here m is static variable that was defined at the class level, and doesn’t go out of scope until the class is destroyed (when the Main() method terminates, and the program ends).

   Variable n is instance variable which can be declared within main method.

   Scope of Variable x and y is within function in which it is passed. It will not create the copy of variable. It work in original location of x and y.

   Variable a is a local variable. It can be used within the function only.


v Boxing and Unboxing

   A reference of type object can be used to refer to any other type, including value types. When an object reference refers to a value type, a process known as boxing occurs.

1.     Boxing :- “Conversion from value type to reference type is called Boxing”

   Boxing causes the value of a value type to be stored in an object instance. Thus, a value type is “boxed” inside an object. This object can then be used like any other object. In all cases, boxing occurs automatically. You simply assign a value to an object reference.

1.     Unboxing :- “Conversion from reference type to value type is called                    Unboxing”

   Unboxing is the process of retrieving a value from a boxed object. This action is performed using an explicit cast from the object reference to its corresponding value type.

   Here is a simple example that illustrates boxing and unboxing:

using System;

class BoxingDemo
 {
static void Main()
 {
int x;
object ob;
x = 10;
ob = x; // box x into an object
int y = (int)ob; // unbox ob into an int
Console.WriteLine(y);
}
}

   This program displays the value 10. Notice that the value in x is boxed simply by assigning it to ob, which is an object reference. The integer value in ob is retrieved by casting ob to int.



0 comments:

Post a Comment

Subscribe us