0% found this document useful (0 votes)
4 views

C#Unit 2

Uploaded by

rutujaphalke36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C#Unit 2

Uploaded by

rutujaphalke36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

UNIT-2

C# Basics
-Miss Mali
Runali.
-Miss Mali Runali.
What to study

Introduction Data Types Control Structures


• Data Types
• Introduction
• Type casting
• Entry point Method
• Boxing And unboxing.
• Different Valid forms of main()
• Partial Class
• Parameter Passing Mechanism

-Miss Mali Runali.


Introduction
A C# program consists of the following parts:
using System;
 Namespace declaration namespace HelloWorldApplication
 A class {
class HelloWorld
 Class methods {
 Class attributes static void Main(string[] args)
{
 A Main method /* my first program in C# */
 Statements and Expressions Console.WriteLine("Hello World");
Console.ReadKey();
 Comments }
Let us look at a simple code that prints the words "Hello World": }
}

-Miss Mali Runali.


 using System; - the using keyword is used to include the System namespace in the program.
A program generally has multiple using statements.

 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.

-Miss Mali Runali.


 The next line /*...*/ is ignored by the compiler and it is put to add comments in the program.

 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 worth to note the following points:


•  C# is case sensitive.
•  All statements and expression must end with a semicolon (;).
•  The program execution starts at the Main method.

-Miss Mali Runali.


C# is an object-oriented programming language. And procedural programming
language .
C# is a high-level language that is designed to be easy to read and write.
In Object-Oriented Programming methodology, a program consists of various objects
that interact with each other by means of actions.
The actions that an object may take are called methods.
Objects of the same kind are said to have the same type or, more often, are said to be
in the same class.
C# can be used with a variety of development tools, including Visual Studio, which is
a popular integrated development environment (IDE) for Windows development.
C# code can be compiled into an executable file or a library, which can be used by
other applications.
Overall, C# is a powerful and versatile programming language that is well-suited for
developing a wide range of applications for the Windows platform.
-Miss Mali Runali.
ENTRY POINT METHOD IN C#.NET

C# applications have an entry point called Main Method.

It is the first method which gets invoked whenever an application started and it is present in every C# executable file.

The application may be Console Application or Windows Application.

The most common entry point of a C# program is

static void Main() or static void Main(String []args).

-Miss Mali Runali.


With Command line arguments:
Without Command line arguments:
This can accept n number of array type parameters during the runtime.
It is up to the user whether he wants to take command line arguments or not. If there
using System;
class Sample is a need for command-line arguments then the user must specify the command
{ line arguments in the Main method.
using System;
// Main Method class Sample
static public void Main(String[] args) {
{ // Main Method
static public void Main()
Console.WriteLine("Main {
Method");
Console.WriteLine("Main Method");
}
}
} }

Applicable Access Modifiers:


public, private, protected, internal, protected internal access Without any access modifier:
modifiers can be used with the Main() method. The default access modifier is private for a Main() method .
The private protected access modifier cannot be used with it.
using System;
using System;
class Sample {
class Sample {
// Main Method without any access modifier
// Main Method
static void Main()
protected static void Main()
{
{
Console.WriteLine("Main Method");
Console.WriteLine("Main Method");
}
} }
}

-Miss Mali Runali.


Order of Modifiers:
Return Type:
The user can also swap positions of static and applicable modifiers in
The Main Method can also have integer return type. Returning an integer
Main() method..
value from Main() method cause the program to obtain a status
using System; information.

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");

// for successful execution of code


return 0;
}
}

-Miss Mali Runali.


Overloading of Main() method is allowed.
*But in that case, only one Main() method is considered as one entry point to start the execution of the program .

// C# program to demonstrate the Valid overloading of Main() method


using System;
class Sample
{

// Main method it can also be written as static void Main(String []args)


static void Main()
{
Console.WriteLine("Main Method");
Main(10);
}

static void Main(int n) // overloaded Main() Method


{
Console.WriteLine("Overloaded Main Method“+n);
Main(10,20)
}

static void Main(int x, int y) // overloaded Main() Method


{
Console.WriteLine("Overloaded Main Method“+x+y);
}
} -Miss Mali Runali.
Command Line Argument
namespace Example3
{
class Program
{
static void Main(string[] args)
{
int num1, num2, result;
num1 = Convert.ToInt32(args[0]);
num2 = Convert.ToInt32(args[1]);
result = num1 * num2;
Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
}
}
-Miss Mali Runali.
}
Write a program in which accept two argument as parameter from the user and returns four output value as add, subtract, multiplication and division.

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();
}
}
}

-Miss Mali Runali.


Q.With Command Line pass your Name(String)

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();
}
}
}

-Miss Mali Runali.


Parameter Passing Mechanism
Parameters are means of passing values to a method.

There are four different ways of passing parameters to a method in C# which are as:
1.Value
2.Ref (reference)
3.Out (reference)

-Miss Mali Runali.


1.Passing parameter By Value

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.

2.Passing parameter by ref

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.

-Miss Mali Runali.


Passing parameter By Value
class Class7
{
static void Main(string[] args) When you pass a value-type variable from one method to
{ another, the system creates a separate copy of a variable in
int i = 100; another method.
Console.WriteLine(i); If value got changed in the one method, it wouldn't affect
ChangeValue(i); the variable in another method.
Console.WriteLine(i);
Console.ReadLine();
}
static void ChangeValue(int x)
{
x = 200;
Console.WriteLine(x);
}

} -Miss Mali Runali.


Passing parameter by ref
using System;
namespace ref_parameter
{
class Class8
{

public int sum(ref int a, ref int b)


{
a = a + 10;
b = b + 20;
return (a + b);
}

static void Main(string[] args)


{
// local data members have to initialized as they are not initiated with class constructor
int a=10 , b=20 ;
Class8 obj = new Class8();
Console.WriteLine("sum of a and b is : " + obj.sum(ref a, ref b));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.ReadLine();
}
}
-Miss Mali Runali.
}
3.Passing parameter by out
• Like reference parameters, output parameters don't create a new storage location and are
passed by reference. It requires out keyword in front of variables to identify in both actual and
formal parameters.

• 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.

-Miss Mali Runali.


using System;
namespace out_parameter
{
class outEg
{
public int sum(int a, int b, out int c, out int d)
{
c = a + b;
d = c + 100;
return (a + b);
}
static void Main(string[] args)
{
int a = 10, b = 20,c,d; // local data members have to initialized as they are not initiated with class constructor
And the out data members doesn't need to assign initial value as they are processed and brought back
outEg obj = new outEg();
Console.WriteLine("sum of a and b is : " + obj.sum(a, b, out c, out d));
Console.WriteLine("Value of a is : " + a);
Console.WriteLine("Value of b is : " + b);
Console.WriteLine("Value of c is : " + c);
Console.WriteLine("Value of d is : " + d);
Console.ReadLine();
}
} -Miss Mali Runali.
}
ref out

ref is a mechanism of parameter passing by reference Out is also a mechanism of parameter passing by reference

A variable to be sent as out parameter don't need to be


A variable to be sent as ref parameter must be initialized.
initialized

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.

-Miss Mali Runali.


Data Types
The Datatypes in C# are basically used to store the data temporarily in the computer through a program

-Miss Mali Runali.


Data Types:

string stringVar = "Hello World!!";


int intVar = 100;
float floatVar = 10.2f;
char charVar = 'A';
bool boolVar = true;

-Miss Mali Runali.


• A data type is a value type if it holds a data value within its own memory space. It means
the variables of these data types directly contain values.

• 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!!";

-Miss Mali Runali.


Float: It is 32-bit single-precision floating point type. It
has 7 digit Precision. To initialize a float variable, use the
suffix f or F. Like, float x = 3.5F;. If the suffix F or f will
not use then it is treated as double.

Double:It is 64-bit double-precision floating point type.


It has 14 – 15 digit Precision. To initialize a double
variable, use the suffix d or D. But it is not mandatory to
use suffix because by default floating data types are the
double type.

-Miss Mali Runali.


Type Casting
1.implicit Type conversion:
In implicit type conversion, the C# compiler automatically converts
one type to another.
Generally, smaller types like int (having less memory size) are automatically
converted to larger types like double (having larger memory size).

-Miss Mali Runali.


using System;

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;

// get type of numDouble


numDouble Type:
Type n1 = numDouble.GetType();
System.Double
// Value before conversion
Console.WriteLine("numInt value: "+numInt);
Console.WriteLine("numInt Type: " + n);

// Value after conversion


Console.WriteLine("numDouble value: "+numDouble);
Console.WriteLine("numDouble Type: " + n1);
Console.ReadLine();
}
}
}

-Miss Mali Runali.


2.Explicit Type Conversion

In explicit type conversion, we convert one type to another.

Generally, larger types like double (having large memory size) are converted to smaller types

like int (having small memory size).

-Miss Mali Runali.


using System;
Original double value: 1.23
namespace MyApplication { Converted int value: 1
class Program {
static void Main(string[] args) {

double numDouble = 1.23;

// Explicit casting
int numInt = (int) numDouble;

// Value before conversion


Console.WriteLine("Original double Value: "+numDouble);

// Value before conversion


Console.WriteLine("Converted int Value: "+numInt);
Console.ReadLine();
}
}
}
-Miss Mali Runali.
Boxing & Unboxing
C# has two kinds of data types, value types and reference types. Value type stores the
value itself, whereas the reference type stores the address of the value where it is stored.
While working with these data types, you often need to convert value types to
reference types or vice-versa.
These conversion processes are called boxing and unboxing.

-Miss Mali Runali.


Boxing
It is the process of converting a value type to the object type or any interface type
implemented by this value type. Boxing is implicit.

int i = 10;
object o = i; //performs boxing

In the above example, the integer variable i is assigned to object o.


Since object type is a reference type and base class of all the classes in C#, an int can be
assigned to an object type.
This process of converting int to object is called boxing.

A boxing conversion makes a copy of the value. So, changing the value of one variable will
not impact others.

-Miss Mali Runali.


All the reference types stored on heap where it contains the address of the value and value type is just an
actual value stored on the stack. Now, as shown in the first example, int i is assigned to object o.
Object o must be an address and not a value itself. So, the CLR boxes the value type by creating a new
System.Object on the heap and wraps the value of i in it and then assigns an address of that object to o.
So, because the CLR creates a box on the heap that stores the value, the whole process is called 'Boxing'.

-Miss Mali Runali.


Unboxing
It is the reverse of boxing. It is the process of converting a reference type to
value type. Unboxing extract the value from the reference type and assign it to a
value type.
Unboxing is explicit. It means we have to cast explicitly.

object o = 10;
int i = (int)o; //performs unboxing

-Miss Mali Runali.


// C# implementation to demonstrate the Boxing
using System;
class Demo {

// Main Method
static public void Main()
{

// assigned int value 20 to num


int num = 20;

// boxing
object obj = num;

// value of num to be change


num = 10;

System.Console.WriteLine ("Value - type value of num is : {0}", num);


System.Console.WriteLine ("Object - type value of obj is : {0}", obj);
}
}

-Miss Mali Runali.


// C# implementation to demonstrate the Unboxing
using System;
class Demo {

// Main Method
static public void Main()
{

// assigned int value 5 to num


int num = 5;

// boxing
object obj = num;

// unboxing
int i = (int)obj;

Console.WriteLine("Value of ob object is : " + obj);


Console.WriteLine("Value of i is : " + i);
}
}

-Miss Mali Runali.


Thank
You…

-Miss Mali Runali.

You might also like