Access Specifier
Access Specifier
• Public
• Private
• Protected
• Internal
• Protected internal
namespace RectangleApplication {
class Rectangle {
//member variables
public double length;
public double width;
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
The member function Display() and GetArea() can also access these
variables directly without using any instance of the class.
namespace RectangleApplication {
class Rectangle {
//member variables
private double length;
private double width;
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
In the preceding example, the member variables length and width
are declared private, so they cannot be accessed from the function
Main(). The member functions AcceptDetails() and Display() can
access these variables. Since the member
functions AcceptDetails() and Display() are declared public, they
can be accessed from Main() using an instance of the Rectangle
class, named r.
Live Demo
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
internal double length;
internal double width;
double GetArea() {
return length * width;
}
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
Length: 4.5
Width: 3.5
Area: 15.75
Defining Methods in C#
When you define a method, you basically declare the elements of its
structure. The syntax for defining a method in C# is as follows −
Example
Following code snippet shows a function FindMax that takes two
integer values and returns the larger of the two. It has public access
specifier, so it can be accessed from outside the class using an
instance of the class.
class NumberManipulator {
return result;
}
...
}
Calling Methods in C#
You can call a method using the name of the method. The following
example illustrates this −
Live Demo
using System;
namespace CalculatorApplication {
class NumberManipulator {
public int FindMax(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
You can also call public method from other classes by using the
instance of the class. For example, the method FindMax belongs to
the NumberManipulator class, you can call it from another
class Test.
Value parameters
This method copies the actual value of an argument into the formal parameter of the
1
function. In this case, changes made to the parameter inside the function have no effect
on the argument.
Reference parameters
2 This method copies the reference to the memory location of an argument into the formal
parameter. This means that changes made to the parameter affect the argument.
Output parameters
3
This method helps in returning more than one value.
C# Constructors
A class constructor is a special member function of a class that is
executed whenever we create new objects of that class.
Live Demo
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
public Line() {
Console.WriteLine("Object is being created");
}
public void setLength( double len ) {
length = len;
}
public double getLength() {
return length;
}
static void Main(string[] args) {
Line line = new Line();
A default constructor does not have any parameter but if you need, a
constructor can have parameters. Such constructors are
called parameterized constructors. This technique helps you to assign
initial value to an object at the time of its creation as shown in the
following example −
Live Demo
using System;
namespace LineApplication {
class Line {
private double length; // Length of a line
Live Demo
using System;
namespace InheritanceApplication {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape {
public int getArea() {
return (width * height);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
Total area: 35
Live Demo
using System;
namespace RectangleApplication {
class Rectangle {
//member variables
protected double length;
protected double width;
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5