Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

C# LESSON 5

Jalil Verdiyev
TOPICS

• Pass by value and reference


• Some Array and String methods
and properties

2
PASS BY VALUE AND
REFERENCE
In c#, while you are calling a function and giving some
parameters, there are 2 ways that your parameters are sent to
the function. As we know that we have 2 data types: Value
type and reference types, for these 2 different types there are
different behaviors.

• Passing value types by value


• Passing reference types by value
• Passing value types by reference
• Passing reference types by reference
PASSING VALUE TYPES BY
VALUE
While passing a value type by value, the c# compiler creates
the copy of original variable and whatever changes are made
to copy, don't affect original.

int a = 20;
Square(a);

// Will print 20
Console.WriteLine(a);

void Square(int num)


{
num = num * num;
}
PASSING
REFERENCE TYPES
BY VALUE
While passing reference types by value
there are two cases:
• If the method assigns the parameter to string[] names = { "Asiman", "Zeynal", "Rahib", "Laman", "Amin" };
refer to a different object, those
changes aren’t visible from the caller. ChangeName(names);
• If the method modifies the state of the
object referred to by the parameter, foreach (string name in names)
those changes are visible from the {
caller. Console.WriteLine(name);
}

void ChangeName(string[] arr)


{
// Will change "Asiman" to "Jalil"
arr[0] = "Jalil";
// Won't change ;/
arr = new string[] { "Elchin" };
}
PASSING VALUE
TYPES BY
REFERENCE
When you pass some arguments by reference, c# doesn't make
a copy, instead it sends the actual variable, so if you make
changes they will affect to original variable, and for that
purpose we need a special keyword ref.

int a = 20;

Console.WriteLine($"Value before Square():{a}");//


20
Square(ref a);
Console.WriteLine($"Value after Square():{a}");//
400

void Square(ref int num)


{
num = num * num;
}
PASSING REFERENCE
TYPES BY REFERENCE
string[] names = { "Asiman", "Zeynal", "Rahib", "Laman", "Amin" }

• If the method assigns the parameter to refer ChangeName(ref names);


to a different object, those foreach (string name in names)
changes are visible from the caller. {
• If the method modifies the state of the object Console.WriteLine(name);
referred to by the parameter, those }
changes are visible from the caller. void ChangeName(ref string[] arr)
{
// Will change "Asiman" to "Jalil"
Rather than the ref keyword there arr[0] = "Jalil";
are two ways of passing an // Will change the whole array
argument by reference: arr = new string[] { "Elchin" };
• in keyword }
• out keyword
Let's look what are the differences
and why they are exists.
IN
KEYWORD
In keyword, makes sure of that the parameter isn't changed
inside the method. It only gives access to read not to write.
While using the in the variable must initialized before
sending to method

string country = "Azerbaijan";

ReadCountry(country);

void ReadCountry(in string countryName)


{
// Ok
Console.WriteLine("Inside method: {0}", countryName);
// Will generate error, It isn't easy to leave Azerbaijan ;)
countryName = "America";
}
OUT
KEYWORD
Out keyword, states that the parameter must be changed inside
the method. And it allows not initializing.

string country;

ReadCountry(out country);

Console.WriteLine(country);

void ReadCountry(out string countryName)


{
// Won't generate error
countryName = "America";
}
void ReadCountry(out string countryName)
{
// Will generate error
}
ARRAY PROPERTIES

Length Rank

string[] strings = { "a", "b", "c" };


string[] strings = { "a", "b", "c" };
// Gives 3 string[,,] strings1 = {
Console.WriteLine(strings.Length); { { "a", "b" }, { "c", "d" }, { "e", "f" } },
{ { "a", "b" }, { "c", "d" }, { "e", "f" } }
};
//Will give 1
Console.WriteLine(strings.Rank);
//Will give 3
Console.WriteLine(strings1.Rank);
ARRAY METHODS

Clear() -> sets all elements to default values


GetValue() -> gets the specified element by index
SetValue() -> sets the specified element by index
IndexOf() -> searches for the given element and returns the index
Resize() -> changes the length of array(decreases or increases)
Reverse() -> reverses the order of elements
Sort() -> sorts the array
STRING PROPERTIES

Length
As in array it returns the count of characters in a string

string name = "Jalil";


// Will print 5
Console.WriteLine(name);
STRING METHODS

1 2 3 4 5 6 7 8 9 10

Compare() Concat() -> Contains() - EndsWith() - Equals() -> Join() -> ToLower(), Trim(), PadLeft(), and all
->compares, merges two >checks if the > returns true checks if the turns array ToUpper() TrimEnd(), PadRight() methods of
the given strings given string if the string string equals into string TrimStart() Array
strings is in the ends with the to given one
actual string given value

You might also like