Properties, Array, Encapsulation: Dept. of Computer Science Faculty of Science and Technology
Properties, Array, Encapsulation: Dept. of Computer Science Faculty of Science and Technology
1. Properties
2. OOP principles
3. Encapsulation
4. Array
Encapsulation
Encapsulation
Encapsulate the inner details of implementation
Protect data
Encapsulation support in C#
// Accessor
public string GetFullName()
{ enforcing encapsulation
return fullName; using traditional
accessors and mutators
}
//Mutator
public void SetFullName (string s)
{
fullName = s;
}
}
Class properties
§ NET languages use properties to enforce encapsulation
§ Stimulate public accessible data
}
Property visibility
// The get and set logic is both public, // Object users can only get the value,
// given the declaration of the property. // however derived types can set the value.
public string SocialSecurityNumber public string SocialSecurityNumber
{ {
g et { return empSSN; } g et { return empSSN; }
se t { empSSN = value; } protected s et { empSSN = value; }
} }
}
Access Modifiers
Access
Short Any class(inherits previous
Modifier Same Class Different Class
Name assembly class)
Type
Protected
PI Yes Yes Yes
Internal
Method Parameter Modifiers
2. The number of dimensions and the length of each dimension are established
when the array instance is created. These values can't be changed during the
lifetime of the instance.
3. The default values of numeric array elements are set to zero, and reference
elements are set to null.
ARRAY OVERVIEW
An array has the following properties:
4. A jagged array is an array of arrays, and therefore its elements are reference
types and are initialized to null.
5. Arrays are zero indexed: an array with n elements is indexed from 0 to n-1.
6. Array elements can be of any type, including an array type.
7. Array types are reference types derived from the abstract base type Array.
ARRAYS AS OBJECTS
• In C#, arrays are actually objects, and not just addressable regions of
contiguous memory as in C and C++.
• Array is the abstract base type of all array types. We can use the properties,
and other class members, that Array has.
int[] numbers = { 1, 2, 3, 4, 5 };
int lengthOfNumbers = numbers.Length;
ARRAY OVERVIEW
• This example uses the Rank property to display the number of dimensions of an array.
class TestArraysClass
{
static void Main()
{ // Declare and initialize an array:
int[,] theArray = new int[5, 10];
System.Console.WriteLine("The array has {0} dimensions.", theArray.Rank);
}
}
• An array that stores string elements can be declared in the same way. For example:
• It is also possible to use initializers to fill the array elements with values, in
which case you do not need the array size.
jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };
JAGGED ARRAYS
• We can also initialize the array upon declaration like this:
• We can access individual elements as shown in this example, which displays the value
of the element [1,0] of the first array (value 5) :
N.B. However, with multidimensional arrays, using a nested for loop gives you more
control over the array elements.
PASSING ARRAYS AS ARGUMENTS
• Arrays can be passed as arguments to method parameters.
• Because arrays are reference types, the method can change the value of the
elements.
• We can pass an initialized single-dimensional array to a method.
int[] theArray = { 1, 3, 5, 7, 9 };
PrintArray(theArray);
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Print2DArray(theArray);
• The following code shows a partial declaration of a print method that accepts a two-
dimensional array as its argument.
• The enum keyword is used to declare an enumeration, a distinct type that consists of
a set of named constants called the enumerator list.
• By default, the first enumerator has the value 0, and the value of each successive
enumerator is increased by 1.
• For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.
• But now we are relying upon users to enter just the right value for that string.
• So now,
• We can clearly specify for client code which values are valid for the variable.
• The main benefit of this is that constants can be referred to in a consistent, expressive
and type safe way.
• Whenever there are situations where you are using a set of related numbers in a
program, consider replacing those numbers with enums. It will make a program more
readable and type safe.
Thank You
Books
• C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010
• Head First C# by Andrew Stellman
• Fundamentals of Computer Programming with CSharp – Nakov v2013
References
C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010