C#Unit 2
C#Unit 2
C# Basics
-Miss Mali
Runali.
-Miss Mali Runali.
What to study
The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication
namespace contains the class HelloWorld.
The next line has a class declaration, the class HelloWorld contains the data and method definitions that your
program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the
HelloWorld class has only one method Main.
The next line defines the Main method, which is the entry point for all C# programs. The Main method states what
the class does when executed.
The Main method specifies its behavior with the statement Console.WriteLine("Hello World");
WriteLine is a method of the Console class defined in the System namespace. This statement causes the message
"Hello, World!" to be displayed on the screen.
The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it
prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
It is the first method which gets invoked whenever an application started and it is present in every C# executable file.
class Sample The value which is returned from Main() method is treated as the exit code
{ for the process.
using System;
// Main Method class Sample
public static void Main() {
{ static int Main() // Main Method with int return
type
Console.WriteLine("Main Method"); {
}
} Console.WriteLine("Main Method");
namespace Example2
{
class Program
{
public static void parameter(int num1, int num2, out int add, out int sub, out int mul, out float div)
{
add = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = (float)num1 / num2;
}
static void Main(string[] args)
{
int num1, num2;
int add, sub, mul;
Float div;
Console.Write("Enter 1st number\t");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("\nEnter 2nd number\t");
num2 = Convert.ToInt32(Console.ReadLine());
Program.parameter(num1, num2, out add, out sub, out mul, out div);
Console.WriteLine("\n\n{0} + {1} = {2}", num1, num2, add);
Console.WriteLine("{0} - {1} = {2}", num1, num2, sub);
Console.WriteLine("{0} * {1} = {2}", num1, num2, mul);
Console.WriteLine("{0} / {1} = {2}", num1, num2, div);
Console.ReadLine();
}
}
} -Miss Mali Runali.
using System;
namespace FirstProgram
{
class Program
{
static void Main(string[] args)
{
//convert into integer type
int argument1 = Convert.ToInt32(args[0]);
Console.WriteLine("Argument in Integer Form : " + argument1);
//convert into double type
double argument2 = Convert.ToDouble(args[1]);
Console.WriteLine("Argument in Double Form : " + argument2);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace command
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("First Name is " + args[0]);
Console.WriteLine("Last Name is " + args[1]);
Console.ReadLine();
}
}
}
There are four different ways of passing parameters to a method in C# which are as:
1.Value
2.Ref (reference)
3.Out (reference)
By default, parameters are passed by value in C#. When arguments are passed to parameters by value, a
new storage location is created for the parameter variable and the value of the argument variable is copied to
this location.
If you copy a value type variable to another variable, the value is copied directly. Then both variables work
independently.
Passing parameters by ref uses the address of the actual parameters to the formal parameters. It requires
ref keyword in front of variables to identify in both actual and formal parameters.
The process of ref is bidirectional i.e. we have to supply value to the formal parameters and we get back
processed value.
• The process of out is unidirectional i.e. we don't have to supply value to the formal parameters
but we get back processed value.
ref is a mechanism of parameter passing by reference Out is also a mechanism of parameter passing by reference
ref is bidirectional i.e. we have to supply value to the formal Out is a unidirectional i.e. we don't supply any value but we
parameters and we get back processed value. get back processed value.
• Unlike value types, a reference type doesn't store its value directly. Instead, it stores the
address where the value is being stored. In other words, a reference type contains a pointer
to another memory location that holds the data.
Eg: string s = "Hello World!!";
namespace MyApplication {
class Program {
static void Main(string[] args) {
int numInt = 500;
numInt value: 500
// get type of numInt
Type n = numInt.GetType(); numInt Type: System.Int32
// Implicit Conversion numDouble value: 500
double numDouble = numInt;
Generally, larger types like double (having large memory size) are converted to smaller types
// Explicit casting
int numInt = (int) numDouble;
int i = 10;
object o = i; //performs boxing
A boxing conversion makes a copy of the value. So, changing the value of one variable will
not impact others.
object o = 10;
int i = (int)o; //performs unboxing
// Main Method
static public void Main()
{
// boxing
object obj = num;
// Main Method
static public void Main()
{
// boxing
object obj = num;
// unboxing
int i = (int)obj;