c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.233 seguidores
Pré-visualização50 páginas
builder.Append(fill);
return builder.ToString();
}
public	static	string	FillLeft(this	string	text,	int	length,	char	fill)
{
if (text.Length >= length) return text;
length -= text.Length;
StringBuilder builder = new StringBuilder(length);
while	(builder.Length	<	length)	builder.Append(fill);
return builder.ToString() + text;
}
public	static	string	FillCenter(this	string	text,	int	length,	char	fill)
{
if (text.Length >= length) return text;
return
FillRight(FillLeft(text,	(text.Length	+	length)	/	2,	fill),	length,	fill);
}
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
190 
Part 3 Collection classes
Part 3 Collection classes
All modern languages, including C# has a selection of collection classes. A collection class can be thought 
of as a container that can contain objects. The individual collection classes differ in how they organize 
objects, and what you can do with them, ie which access there is to the content of the container. The 
following describes the most important collection classes that are contained within the .NET framework.
.NET is born with commonly used collection classes fully implemented and ready to use. They are all 
implemented as generic classes, and I will in this part of the book mention the following:
\u2022	 List<T>
\u2022	 Stack<T>
\u2022	 Queue<T>
\u2022	 LinkedList<T>
\u2022	 Dictionary<K, V>
\u2022	 SortedDictionary<K, V>
I will only focus on how the classes can be used, and for a full documentation of the classes and their 
methods, I will refer to the documentation on MSDN.
Before addressing it, I will briefly recall an array, which also is a container. Consider as an example the 
following program:
using System;
namespace Exam44
{
class Program
{
static void Main(string[] args)
{
int[] t = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
string[] s = new string[6];
s[0] = &quot;Svend&quot;;
s[1] = &quot;Valborg&quot;;
s[2] = &quot;Knud&quot;;
s[3] = &quot;Abelone&quot;;
s[4] = &quot;Valdemar&quot;;
s[5] = &quot;Gudrun&quot;;
Array.Reverse(t);
Array.Sort(s);
foreach (int n in t) Console.Write(&quot;{0} &quot;, n);
Console.WriteLine();
foreach (string v in s) Console.Write(&quot;{0} &quot;, v);
Console.WriteLine();
}
}
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
191 
Part 3 Collection classes
If you run the program you get the following result:
There is not much mystery in it, and you should especially note the class Array, which make a number 
of static methods available to manipulate arrays.
In most cases, an array could be an alternative to a collection class, and the most collection classes are actually 
an encapsulation of an array. There is no problem in using arrays instead of collection classes, but conversely 
gives collection classes are great advantages. For example has an array a fixed size and it is up to the programmer 
to keep track of where each element is and whether there is room for it. Is there not room, it is necessary to 
create a larger array and copy the content of the old array to the new one. It is all that logic that is encapsulated 
in a collection class, and thus something that happens automatically behind the programmer\u2019s back. Besides 
that, the collection classes make different services available that fits typical applications.
As mentioned above, the collection classes are generic and thus have a strong type. That is, the objects 
as a concrete collection may contain, must be of a certain type. However, there is also a selection of 
non-generic collection classes:
\u2022	 ArrayList
\u2022	 Stack
\u2022	 Queue
\u2022	 SortedList
\u2022	 Hashtable
which are collections where objects have the type object. These classes are derived from the first versions 
of the .NET framework, which had not generic types available. The classes are still available and can 
be used, but it can be recommended to use the newer generic versions, since the result is a more stable 
code, because the compiler can test the type of the objects.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
192 
List<T>
24 List<T>
This type can best be interpreted as a dynamic array that is a type which in many ways can be used as 
an array, but may grow dynamically as needed. This is in contrast to an ordinary array, where you have 
to specify how many elements there must be room for when the array is created. One must not draw 
the comparison too far, and another interpretation is to think of the type as a sequence of elements, 
where you can always add elements to the end of the list. A List is a structure that at a given time has 
a capacity, and then you can add elements to the end of the list. The picture could be as shown below, 
where the capacity is 15, while there are 8 places that are used:
2 3 5 7 11 13 17 19
You can add elements where the arrow is pointing, and if at some point you exceed the capacity, it will 
automatically be extended. This is done by doubling the capacity.
Besides that you can add elements to the end of a List, the class offers a number of other methods, for 
example that one can delete an item, inserting an item in a certain place, etc. Internally, a List is an 
array, and it also means that you should be aware of the complexity of different methods. A List can 
not have empty places, and that means that if, for example you delete an item, all elements to the right 
of the deleted element must be moved one place to the left. Similarly, if you insert an element all the 
elements to the right of the location where the element is inserted, must be moved one place to the right 
to make room for the new element. In contrast the Add() that adds one element to the end of the list, 
is particularly effective since it can immediately place the element where the arrow is pointing \u2013 except 
in the situation where it is necessary to double the capacity.
Exam45
A List of strings
As an example the below program shows a method that creates a list of strings:
static void Main(string[] args)
{
List<string> navne = new List<string>();
Console.WriteLine(navne.Capacity);
navne.Add(&quot;Svend&quot;);
Console.WriteLine(navne.Capacity);
navne.Add(&quot;Knud&quot;);
navne.Add(&quot;Valdemar&quot;);
Console.WriteLine(navne.Capacity);
Console.WriteLine(navne.Count);
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
193 
List<T>
navne.Insert(0, &quot;Olga&quot;);
navne.Insert(0, &quot;Gudrun&quot;);
Console.WriteLine(navne.Capacity);
Console.WriteLine(navne.Count);
navne.Remove(&quot;Knud&quot;);
navne.RemoveAt(1);
Console.WriteLine(navne.Capacity);
Console.WriteLine(navne.Count);
for (int i = 0; i < navne.Count; ++i) Console.WriteLine(navne[i]);
}
With us you can 
shape the future. 
Every single day. 
For more information go to:
www.eon-career.com
Your energy shapes the future.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
194 
List<T>
Explanation
First the program print the capacity of the list that at the start is 0. However, it is possible to create a list, 
using as a parameter to the constructor that sets the starting capacity. After the addition of an element, 
the program prints the capacity again. It is now 4, which means that the first time you add an item to 
the list it allocates space for 4 elements. After added two more elements and prints the capacity again 
and the number of elements, they are respectively 4 and 3, which is not surprising. As a next step the 
program insert two elements at the beginning of the list (in position 0) with the method Insert(). The 
capacity is then 8 (is doubled), and the number of elements is 5. Then the program deletes two elements, 
and the capacity is still 8. It should be noted that a list not automatically shrinks. The last for loop print 
all elements in the list,