Unie 1 Funeitons, Arrays andl St7ngs
nit IL
ys and Strings
* String—
In C#, string is an object of System.String class that represents se
‘We can perform many operations on strings such as concatenation,
substring, search, trim, replacement ete :
quence of characters.
comparison, getting
string vs String —
In Cé, string is keyword which is an alias for System.String class. That
String are equivalent. We are free to use any naming convention.ons, Arrays ond s,,.
ns
the same value as
ave the
specified String objects hi
c shether two
js used to check whe otherwise fulse.
hhave same value, it return true
ee rye CF aval) method
same vale or no. oth srngs
Remove(Int beginindex, Int length) ~ ; : ee
“ The ica ) method is used to get a new string after removing all the chi
from specified beginIndex till given length. If length is not
characters after begintndex.
specified, it removes all the
7) Replace(string s1, string s2) ~
‘The C# Replace( ) method is used to yet a new string in which all occurrences of a
specified Unicode character in this string are replaced with another specified Unicode character.
Itreturnsa string
8) Substring(Int startIndex, Int length) —
‘The C# SubString( ) method is used to get a substriny
; wg from a String. The substring st
at a specified character position and continucs to the end of | he
the string. It returns a string,
9) ToLower() ~
‘The C# ToLower( ) method is used to convert a string into lowercase, It ret
-Itreturns a st
10) ToStringy ) —
ring.
pperease. It re
12) Trim() — turns a string.
The CH Trim) method j
is used to :
from the curent String object “move all leading and trating veOutput
Compare
Concat — fibedef | Contains
Copy
Replace
Totower abe
ToUpper
namespace stringExample
{
publie partial class Form! : Form
{
public Form1()
{
InitializeComponent();
private void bin_stringclass_Click(object sender, EventArgs €)
{
string s1 = textBox1.Text;
CH.Net [email protected]
Remove [decoy |uCopy ,
txt_copy. Text = string. Copy(s!)s
{Equals
txt_equals. Text = (string. Bquals(s1, s2))-ToStringOs
Mreplace
txt_replace. Text ~s1.Replace(s!, 32);
ITolower
txt_tolower.Text = s1.ToLower();
IMoupper
txt_toupper.Text = s1.ToUpperQs
Ilkemove
tuxt_remove. Text ~ 53.Remove(5, 6);
Hisubstring,
txt_substring.Text = s3.Substring(9,2);
MTostring
intn = 123;
tat_tostring Text = n-ToString0);
akshaypavtegmail.comion i to execute
jg a block of code that perform specified task. Function 1s used to
specified inthe code block. To use a method, you need to ~
1) Define the method
2) Call the method
1) Defining Method in C#- ;
“The syntax for defining 2 method in Ci is as follows —
‘Syntax —
Function Name ( (); |
: (Next the event itself is declared, using the event keyword >
Chet
[email protected]
19{
nitialieComponent(),
private vod b_run_Clek(ojest sender EventArgs &)
{
\ EventClass ec = new BventClass()s
Event Test et = new EventTest();
ec. Status += new Edelegate(et.EventCatch);
cosTriggerEvent(s
}
a
public class EventTest
public void EventCateh(string st)
u
MessayeBox.Show(str);
}
}
public class EventClass
t
public event Edelegate Status;
Itdeclaration of event
public void TriggerEvent()
{
+ if (Status = nul!)
Status("Event Triggered");
[email protected] 2ed easily. and still nae promote the
th only get accessor is a Read only property.
rit Only Properties: A property with only set accessor is a Write only property.
In C#, we use get and set accessors to implement properties.
synten—
t
get {// get statements} // Only get property is read only
set {//set statements} _// Only set property is write only
4/ when get and set then property is read-write
}
Program — Write a program to demonstrate concept of properties in CH.
namespace propertiesExample
{
Public partial class Form! : Form
FA
public Form1( )
{
3 InitializeComponent( );
“shaypawte@ymailcomabiicetna ea
{ ys Ney
int no = 5;
public int noProperty 1 Property definition
{
get { return no; }
set { no = value; }
}
}
}
Old Value -
New Value
Displayly name, Indexers are not defined with
ord, which refers to the object instance.
this[ index]
i
get { // get statements }
1/ Only get property is read only
set { // set statements }
1/ Only set property is write only
J/ when get and set then property is read-write
3
‘The following example demonstrates the concept of Indexer —
SDE Form Hae:
Indexers
Get Values Set Values
RN 20
namespace indexerExample
{
public partial class Forml : Form
C#.Net
[email protected]|= "Sham";
trn.Text = (5{0]).ToString( );
e. Text = (sf 1]).ToString( )s
private void bin_exit_Click(object sender, EventArgs e)
{
}
-Application.Exit( );
}
public class Student 7
(
int stud_m;
string stud_name;
public Student(int rn, string name)
{
this.stud_mn = rm;
this.stud_name = name;
|
public object this[int index}
t
Ch.Net [email protected] Astud_rn = (intvalue;
else if (index == 1)
stud_name ~ (stringvalue;
legate is a type safe fumction, C¥ delegates are similar to point
legate is a reference type variable that holds the reference to a metho
Delegates are especially used for im
delegates are _Stlegates are implicitly derived from the System.Delegate class]
‘Syntax —
Step I: Define a Delegate
Saccess_modifier> delegate (Sparameters>’
Step 2: Create an Instance of a Delegate
= new ( ();
Ch.Ner
akshaypavle@gmailcom
to fun
implementing events and the eall-back m
ni
vi
ame>);4
i
. if Step 2 - Delegate instantiation
a aoe: “Text = (di(amount)) ToString( ): Il Step 3 - Delegate Invocation
}
private void bin_exit_Click(object sender, EventArgs ¢)( “The following program demonstrates multicasting of a delegate ~
: ‘namespace multicastDelegate
{
e ‘public delegate double currencyDelegate(double n);
Sp pati paral clas Form : Form
a {
ic Form||
ae ee ‘orml()
4 InitializeComponent( );
J
private void bin_convert_Click(object sender, EventArgs e)
{
double amount = doubleyParse(txt_rs.Text);
Currency obj = new Currency( );
CHNet [email protected]
nal cern) Tosti},
sender, EventArgs e)
Ss
Public double dollar(double x) i
{ ae
_ return x /'70;
}
=.
public double euro(double x)
‘
return x / 90;
}
}
J
Multicast Delegate
AmostinRs- i
a ‘outa Dot. [i
Awouintvo- arm
(omer
CHNet
[email protected]