c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.228 seguidores
Pré-visualização50 páginas
variables, both types of reference. This means that if you copy a Par, 
it\u2019s these references that will be copied:
static void Test2()
{
Par p1;
p1.d1 = new Dice();
p1.d2 = new Dice();
Console.WriteLine(p1);
Par p2 = p1;
Console.WriteLine(p2);
p1.Throw();
Console.WriteLine(p1);
Console.WriteLine(p2);
}
The first line creates a Par object, and then the variables are initialized to new Dice objects. This is 
necessary because the default constructor does not create these objects and can\u2019t be overridden. In the 
fourth line another Par object is created, which is set equal to p1. Note that this means that the object 
p1 is copied to p2, but what is copied, is the two instance variables, and they thus references to two Dice 
objects. The result is that the two Par objects, each has their own reference variables, but they refer to 
the same Dice objects. It becomes clear if you throw with one Par (p1):
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
157 
Struct
Nullable struct\u2019s
A struct is a value type, and therefore can\u2019t be null, but as shown above, a simple type may be nullable, 
and the same applies for a struct. If Point is the same type as above, consider the following:
static void Test3()
{
Nullable<Point> p = null;
Console.WriteLine(&quot;: &quot; + p);
p = new Point(3.14, 1.41);
Console.WriteLine(&quot;: &quot; + p);
Console.WriteLine(p.Value.Length);
Point v = p.Value;
v.x = 11;
v.y = 13;
Console.WriteLine(&quot;: &quot; + p);
}
Here, p is not a Point but a Nullable<Point>, a Point, which may be null. What happens is that the value 
type is encapsulated in a reference type. Above, p is initially null. Next, p is set to a new Point object, but 
you now have hidden the properties of thetype Point. Instead, we can refer to the encapsulated object 
with the property Value. Note especially that when you write
Point v = p.Value;
you get a copy of the encapsulated object.
One should therefore not make nullable variables , unless you have special needs. Note especially that
int? a
only is a short notation for
Nullable<int> a
The above examples concerning. struct\u2019s are found in Exam37.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
158 
Generic types
20 Generic types
Generic types are attached to the so-called collection classes that are dealt with later, but the goal here 
is to show how to write custom generic types. Short it can be said that the aim is to write types that are 
general and can be used in many contexts. Instead of the generic type one also refers to parameterized 
types corresponding to that it is types that depend on one or more parameters.
Generic methods
Before I show how to write a custom generic type, I will look at a related issue, namely what we mean 
by a generic method. As an example is shown a method to swap two integers of the type int:
static void Swap(ref int a, ref int b)
{
int t = a;
a = b;
b = t;
}
It is a very simple method but a method of great use, for example if you have to sort an array. The method 
has a problem that is closely related to the type int in that way that if you also need a method that can 
swap two objects of the type double, then it is necessary to write a new Swap() method acting on double 
and similarly for all the other types in which there is a need for a Swap() method. It may therefore be 
desirable to write a general method that can handle all types. This is where generic methods come into 
play:
static void Swap<T>(ref T a, ref T b)
{
T t = a;
a = b;
b = t;
}
It\u2019s also called a parameterized method similar to that of the method is associated with a type parameter, 
here called T. In addition to that there after the method name is a <T> it is equivalent to that int 
everywhere is replaced with type parameter T. For example the method can be used as follows, where 
it exchanges two strings:
string s1 = &quot;Svend&quot;;
string s2 = &quot;Knud&quot;;
Swap(ref s1, ref s2);
Console.WriteLine(s1);
Console.WriteLine(s2);
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
159 
Generic types
As another example is below shown a generic method that prints an array:
static void Print<T>(T[] arr)
{
foreach (T t in arr) Console.Write(t + &quot; &quot;);
Console.WriteLine();
}
Note that the code is written entirely as T was an existing type \u2013 a concrete type. However, it has its 
limitations since the only thing you can do with objects of the type T is what you can with an object \u2013 
the compiler can\u2019t impossible have knowledge of other properties of the type T. However, there are 
possibilities to impose restrictions on the type parameter T, so that the compiler can assume certain 
methods or properties.
Get Help Now
Go to www.helpmyassignment.co.uk for more info
Need help with your
dissertation?
Get in-depth feedback & advice from experts in your 
topic area. Find out what you can do to improve
the quality of your dissertation!
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
160 
Generic types
Exam38
Sorting an array
It should be written as a method which can sort an array of objects of any type.
How to
There are many different sorting methods, and here I will use a method that can be described as follows
\u2022	 loop over the array and find the smallest element
\u2022	 swap the smallest element with the element in position 0 \u2013 now the first item is correct
\u2022	 loop over the last n-1 elements and find the smallest among these
\u2022	 swap that element with the element in position 1 \u2013 now the first two elements are correct
\u2022	 loop over the last n-2 elements and find the smallest among these
\u2022	 swap that element with the element in position 2 \u2013 now the first three elements are in place
\u2022	 continue now until the array is sorted \u2013 for each pass find the smallest of the elements that 
are not already in place and swap it to the right position
The result is that after k passes are the first k elements sorted while you still have to sort the last n-k 
elements. It is a very simple sorting method, but it is however not the most effective \u2013 at least not for 
large arrays.
Writing a method for sorting an array of any type, is a too large requirement, since a sorting of the 
elements will always include that the elements can be compared and ranked in order of size. Many of 
the built-in types can be, for example the simple types and the type string that can be compared with 
the comparison operators, but other types can also be compared, and it usually happens in that they 
implement an interface called IComparable. This interface defines only one method called CompareTo(), 
which has an object as a parameter. The protocol is that the method must return -1 if the current 
object is less than the parameter, 1 if the current object is greater than the parameter and otherwise 0. 
The interface is also available in a generic version and the method CompareTo() is thus also a generic 
parameterized with the kind of elements to be compared. The sum of all this is that the sorting method 
can be written as follows:
static void Sort<T>(T[] arr) where T : IComparable<T>
{
for (int i = 0; i < arr.Length - 1; ++i)
{
int k = i;
for (int j = i + 1; j < arr.Length; ++j)
if (arr[j].CompareTo(arr[k]) < 0) k = j;
if (i != k) Swap(ref arr[i], ref arr[k]);
}
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
161 
Generic types
Explanation
The method is simple and expresses the above algorithm, but there are a few important things to note. 
Note first that it is a generic method parameterized with T, and that it has a parameter arr that is an 
array of the type T. Next, note you should note