Professional Documents
Culture Documents
UNIT-2
The Basics and Console Applications inC#
Prof. Naimish R. Vadodariya
naimish.vadodariya@darshan.ac.in
+91-8866215253
class Square }
Console.ReadLine();
}
{ }
p ri v a t e Output :
i n t Side; 1
100
Unit – 2 : The Basics and Console Applications in C# 5 Darshan Institute of engineering &Technology
Example - Constructor
using System; / / co p y constructor
p u b l i c Person(Person person)
namespace Test {
m_PID = person.m_PID;
{ m_FName= person.m_FName;
m_LName= person.m_LName;
p u b l i c class Person m_City = person.m_City;
}
{ }
p u b l i c Person(string f irstName, s t r i n g
lastName)
Unit – 2 : Th{e Basics and Console Applications in C# 6 Darshan Institute of engineering &Technology
Destructor
•
A destructor is a special member function of a
class that is executed whenever an object of its
class goes out of scope.
•
A destructor will have exact same name as the
class prefixed with a tilde (~) and it can neither
return a value nor can it take anyparameters.
•
Destructor can be very useful for releasing
resources before coming out of the program like
closing files, releasing memories etc.
• Aclass can have only one destructor.
Unit – 2 : The Basics and Console Applications in C# 7 Darshan Institute of engineering &Technology
Destructor - Example
using System;
Output :
namespace Destructor
class Example
p u b l i c Example() / / D e f a u l t Constructor
Console.W riteLine("Constructor");
~Example() / / D e s t r u c t o r
Console.ReadKey();
class Program
Memory Management Constructor allocates the memory. Destructor releases the memory.
Constructor has the same name as class Destructor also has the same name as
Name
name. class name but with (~) tiledoperator.
ClassName(Arguments) ~ ClassName()
{ {
Syntex //Body of Constructor //Body of Destructor
} }
Unit – 2 : The Basics and Console Applications in C# 9 Darshan Institute of engineering &Technology
Function (Method) Overloading
•
Function overloading or (method overloading) is
a programming concept that allows programmers
to define two or more functions with the same
name.
•
C# also allows us to define multiple functions
with the same name differing in the argument
type and order of arguments. This is termed as
function overloading.
•
There is no need to use any keyword while
overloading a function or method either in same
Unit – 2clhaesBassicsoanrd CionnsodleAepprliicvatieond
:T s in c
C#lass1.0 Darshan Institute of engineering &Technology
Function Overloading Cont..
•
In case of function or method overloading,
compiler identifies which overloaded method to
execute based on number of arguments and their
data types during compilation itself.
• Hence method overloading is an
Class example for
Unit – 2 : The Basics and Console Applications in C# 11 Darshan Institute of engineering &Technology
Example - Function Overloading
class add
Output :
{
p u b l i c i n t sum(int a , i n t b )
return a + b;
p u b l i c i n t sum(int a )
class Pr{ogram
{ return a + a;
p u b l i c s t a t i c void M a i n ( s t r i n g [ ] ar g s)
}
{
} add ad = new a d d ( ) ; i n t
i = ad.sum(2, 3 ) ;
Console.W riteLine("Addtion i s { 0 } " , i ) ;
i n t b = ad.sum(2);
Console.W riteLine("Addtion i s { 0 } " , b ) ;
Console.Read();
}
Unit – 2}: The Basics and Console Applications in C# 12 Darshan Institute of engineering &Technology
Operator Overloading
•
Each operator has predefined meaning most of them are
given additional meaning through the concept of Operator
Overloading.
•
‘+’ sign is used for addition
• So,‘+’ can not be used for concatenation?
– Suppose, we have String 1 = Ram & String 2 = Rahim
– Can we concat these two strings using + operator like ‘RamRahim’??
•
When any operator is overloaded, keep in mind that its
original meaning is not lost.
Unit – 2 : The Basics and Console Applications in C# 13 Darshan Institute of engineering &Technology
Operator Overloading Cont..
•
Consider an example of user defined data type int
with the operators +, -, * and / provides support
for mathematical operations.
•
To make operations on a user – defined data type
is difficult asthe operations are built –in.
• An operator can be overloaded by defining a
function to it.
• The function is declared using the operator
keyword.
•
The operator function must be static.
Unit – 2 : The Basics and Console Applications in C# 14 Darshan Institute of engineering &Technology
Operator Overloading Cont..
•
For example, to overload the + operator, the
following syntax is defined.
Unit – 2 : The Basics and Console Applications in C# 15 Darshan Institute of engineering &Technology
Operator Overloading Cont..
Operators Description
+, -, !, ~, ++,-- These unary operators take one operand can be overloaded
These binary operators take two operands and can
+, -, *, /, %
be overloaded
==, !=, <, >, <=, >= The comparison operators can be overloaded
The conditional logical operators cannot be overloaded
&&, | | directly and evaluated by using the & and | which can be
overloaded
+=, -=, *=, /==, %== The assignment operators cannot be overloaded
=, ? :, - >, new, sizeof, typeof These operators cannot be overloaded
Unit – 2 : The Basics and Console Applications in C# 16 Darshan Institute of engineering &Technology
Example - Operator Overloading
using System; p u b l i c void ShowResult()
namespace Demo {
Console.WriteLine(a + " , " + b + " , "
{ + c);
class c a l c u l a t i o n Console.ReadLine();
}
{ }
i n t a, b, c;
public c a l c u l a t i on () class Program
{
{ s t a t i c void M a i n ( s t r i n g [ ] args)
a = b = c = 0; {
c a l c u l a t i o n i = new c a l c u l a t i o n ( 1 0 , 20, 3 0 ) ;
}
i++;
public c a l c u l a t i o n ( i n t x , i n t y , i n t z) i.ShowResult();
Console.ReadLine();
{
}
a = x; }
b = y; }
c = z;
}
Output :
p u b l i c s t a t i c c a l c u l a t i o n operator
+ + ( ca l cu l at i o n op1)
{
op1. a++;
op1. b++;
op1.c++;
Output :}
Unit – 2 : The Basics and Console Applications in C# 18 Darshan Institute of engineering &Technology
Modifiers or Specifiers
Access modifiers defines the scope of a class member. A class member
can be variable or function.
Access modifiers are keywords used to specify the declared accessibility
of a member or atype.
Why to use accessmodifiers?
•
Access modifiers are an integral part of object-oriented
programming.
•
They support the concept of encapsulation, which promotes the idea
of hiding functionality.
•
Access modifiers allow you to define who does or doesn't have
access for certain features.
Unit – 2 : The Basics and Console Applications in C# 19 Darshan Institute of engineering &Technology
Modifiers or Specifiers
In C# Modifiers can be divided in five categories.
•
Public Access Specifier
•
Private Access Specifier
•
Protected AccessSpecifier
•
Internal AccessSpecifier
•
Protected Internal AccessSpecifier
Unit – 2 : The Basics and Console Applications in C# 20 Darshan Institute of engineering &Technology
Public Modifier
•
Public is the most common access specifier in C#.
•
It can be access from anywhere, that meansthere
is no restriction on accessibility.
• The scope of the accessibility is inside class
as well asoutside.
• The public keyword is used for it.
•
Accessibility:
– Can be accessed by objects of the class
Unit – 2 : TheBasics and Console Applications in C# 21 Darshan Institute of engineering &Technology
using System;
namespace Demo
{
Example - Public Modifier
class Access
Output :
{
p u b l i c i n t num1;
class Program
s t a t i c void M a i n ( s t r i n g [ ] args)
/ / D i r e c t access t o p u b l i c members
ob1.num1 = 100;
Console.ReadLine();
}
Unit – 2 : The Basics and Console Applications in C# 22 Darshan Institute of engineering &Technology
Private Modifier
•
Private members are accessible only within the
body or scope of the class or the structure in
which they are declared.
•
The private members cannot be accessed outside
the class and it is the least permissive access
level.
• The private keyword is used for it.
•
Accessibility:
Unit – 2
– Cannot be accessed by object
: The Basics and Console Applications in C# 23 Darshan Institute of engineering &Technology
namespace Demo
p u b l i c i n t num1;
p r i v a t e i n t num2;
class Program
s t a t i c void M a i n ( s t r i n g [ ] args)
/ / D i r e c t access t o p u b l i c members
ob1.num1 = 100;
ob1.num2 = 10;
Console.ReadLine();
Unit – 2 : The
}
Basics and Console Applications in C# 24 Darshan Institute of engineering &Technology
Protected - Modifier
•
The protected keyword is also part of the
protected internal and private protected access
modifiers.
•
The scope of accessibility is limited within the
class or struct and the class derived (Inherited )
from this class.
•
A protected member of a base class is accessible
in a derived class only if the access takes place
through the derived classtype.
• The protected keyword is used for it.
Unit – 2 : The Basics and Console Applications in C# 25 Darshan Institute of engineering &Technology
Example - Protected Modifier
using System;
namespace Test
class access
protected i n t age;
p u b l i c void p r i n t ( )
class Program
s t a t i c void M a i n ( s t r i n g [ ] args)
class access
i n t e r n a l i n t age;
p u b l i c void p r i n t ( )
class Program
s t a t i c void M a i n ( s t r i n g [ ] args)
class access
protected i n t e r n a l s t r i n g name;
p u b l i c void p r i n t ( )
class Program
s t a t i c void M a i n ( s t r i n g [ ] args)
{
Unit – 2 : The Bacacseiscssaacnd=CnoewnsaoclceesAsp()p;licationsinC# 30 Darshan Institute of engineering &Technology
Default Access
•
A default access level is used if no access modifier
is specified in a member declaration.
• The following list defines the default access
modifier
C#Types for Description
certain C#types:
enum The default and only access modifier supported is public.
Class The default access for a class is private.
It may be explicitly defined using any of theaccess modifiers.
Interface The default and only access modifier supported is public.
Struct The default access is private with public and internal supported
as well.
Interface and enumeration members are always public and no access modifiers are
allowed.
Unit – 2 : The Basics and Console Applications in C# 31 Darshan Institute of engineering &Technology
Properties
•
In C#, properties are nothing but natural
extension of data fields.
• They are usually known as 'smart fields' in C#
community.
• We know that data encapsulation and hiding are
the two fundamental characteristics of any object
oriented programming language.
• In C#, data encapsulation is possible through
either classes or structures.
•
Usually inside a class, we declare a data field as
Unit – 2 : The Basics and Console Applications in C# 32 Darshan Institute of engineering &Technology
Properties Cont..
•
Properties are special kind of class member, In
properties we use predefined Set and Get
method.
•
They use assessors through which we can read,
written or change the values of the private
fields.
•
We cannot access these fields from outside the
class , but we can accessing these private fields
through properties.
Unit – 2 : The Basics and Console Applications in C# 33 Darshan Institute of engineering &Technology
Properties Cont..
•
A property is a combination of variable and
a method.
• The get method is used to return property value
to the user.
Public <return type> <PropertyName>
{
• The set
get method is used to assign a new valu
{
e to
the property.
}
r e t u r n <var>;
set
• Syntax {
<var> = va l u e ;
}
}
Unit – 2 : The Basics and Console Applications in C# 34 Darshan Institute of engineering &Technology
Example - Properties
class Example class Program
{ {
s t a t i c void M a i n ( s t r i n g [ ] args)
p r i v a t e i n t number;
{
p u b l i c i n t Number
Example example = new Example();
{ / / set { }
example.Number = 5 ;
get
/ / get { }
{ Consol e. Wr i t eLi ne( exampl e. Number ) ;
r e t u r n number; Console.Read();
} }
}
set
{
Output :
number = va l u e ;
}
5
}
Unit – 2 : The Basics and Console Applications in C# 35 Darshan Institute of engineering &Technology
Indexers
•
Indexer is a new concept introduced by C#.
•
An Indexer is a special type of property that
allows a class or structure to be accessed the
same way asarray for its internal collection.
• In short, Indexer Concept is object act as an
array.
• It is same as property except that it defined with
this keyword with square bracket and
parameters.
•
It can be used for overloading a [] operator (Indexers
Unit – 2 : The Basics and Console Applications in C# 36 Darshan Institute of engineering &Technology
Indexers Cont..
Unit – 2 : The Basics and Console Applications in C# 37 Darshan Institute of engineering &Technology
Example - Indexers
class sample class Program
{
{
s t a t i c void M a i n ( s t r i n g [ ] args)
p r i v a t e s t r i n g [ ] name = new s t r i n g [ 3 ] ;
{
sample s = new sample();
s [ 0 ] = "Darshan";
public s tr i n g t h i s [ i n t index]
s[1] = "Institute";
{ s [ 2 ] = "Of Engg. &Tech."; f o r
( i n t i = 0 ; i <= 2 ; i + + )
get
{
{
C o n s o l e .W ri te L i n e ( s [ i ]) ;
Unit – 2 : The B}asics and Console Applications in C# 38 Darshan Institute of engineering &Technology
Properties v/s Indexers
Properties Indexers
Properties don't require this keyword Indexers are created with this keyword
Properties are identified by their names Indexers are identified by signature
Properties are accessed by their names Indexers are accessed using indexes
Properties are also known as the smart fields Indexers are also known as smartarrays
A get accessor of a property has no Indexers in C# must have atleast one
parameters & A set accessor of a property parameter & it also supports more than one
contains the implicit valueparameter. different types of parameters
Syntax : Syntax :
<access_modifier> <return_type> <access_modifier> <return type> this
<property_name> [argument list]
{ {
get { } get { }
set { } set { }
} }
Unit – 2 : The Basics and Console Applications in C# 39 Darshan Institute of engineering &Technology
Introduction to ConsoleApplication
•
A console application is an application that runs
in a console window same asa C,C++etc.
• It doesn’t have any graphical user interface (GUI).
console applications will have character based
interface.
•
To work with console applications in .NET,
Console class is available that is within the
namespace System, which is the root namespace.
Unit – 2 : The Basics and Console Applications in C# 40 Darshan Institute of Engineering & Technology
To create and run aconsole
• Start Visual Studaiop.plication
• On the menu bar, choose FileNewProject.
• The New Project dialog box opens.
•
Expand Installed, expand Templates,
expand Visual C#, and then choose Console
Application.
•
In the Name box, specify a name for your project,
and give location then choose the OKbutton.
• The new project appears in Solution Explorer.
Un•it – 2 : The Basics and Console Applications in C# 41 Darshan Institute of Engineering & Technology
To create and run a consoleapplication
•
Replace the contents of Program.cs with the
f/o/lAoHwelilnogWocrodl d!ep.
Unit – 2 : The Basics and Console Applications in C# 42 Darshan Institute of Engineering & Technology
Thank you