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
Cup with the number of cubes that you want to play with. The game itself is 
carried out by the method Play().
Note here especially the method Yatzy() that tests that all cubes are the same. It is a private method, 
since it only should be used in class Game.
Comment
The class Dice has a property that tells what the cube show:
public int Eyes
{
get { return eyes; }
}
Technically speaking, this means that there automatically is created the following method:
public int get_Eyes()
{
return eyes;
}
A property is thus basically the same as a method but with a different syntax. The goal of a property is 
that the user uses a property as it was a variable, but still with the security that it is the programmer of 
the class that determines what is possible. Above it is thus the programmer of the class Dice, who has 
determined that the user must read a cube eyes, but not change the value.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
88 
Methods
10 Methods
This section elaborates on issues relating class methods, and it is primarily about four things:
\u2022	 methods names
\u2022	 methods return value
\u2022	 properties
\u2022	 methods parameters
and here it is about methods parameters which are most to say.
Methods names
About methods names there are not much to say beyond that is the same as names for variables. However, 
it has been a convention in C# that the name of a method always starts with an uppercase letter. A 
method is identified by its name and its parameters. A class may well have multiple methods with the 
same name as long as they have different parameters, either in terms of number or types.
Realizar o que realmente importa \u2013 
com uma carreira na Siemens.
siemens.com/careers
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
89 
Methods
Exam24
Function overriding
This example shows a program and a class with three methods of the same name.
How to
The class Program has the following methods, all with the same name:
static int Max(int a, int b)
{
return a < b ? b : a;
}
static double Max(double a, double b)
{
return a < b ? b : a;
}
static int Max(int a, int b, int c)
{
return Max(Max(a, b), Max(b, c));
}
Explanation
The compiler may separate the two first, when the two parameters that are of different type, and may 
also separate the last when it has three parameters.
Notice how the ? operator is used. Also note how the last method actually calls the first.
Note that methods can\u2019t be separated on return type, but only on the parameters.
The fact that a method can\u2019t be identified by the name only, but also of its parameters, are sometimes 
called function overloading. Note also that it is not something that is special to methods, but also applies 
to operators. If, for example you write
c = a + b;
it means something different depending on which types of variables a and b are \u2013 for instance if the 
type is an int or a string.
Comment
In this example I have used the question operator. I have previously discussed this operator, but since 
it is an operator, which some feel is hard to understand, it must have an additional comment here. In a 
program you can write something like the following:
if (a < b)
m = a;
else
m = b;
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
90 
Methods
that assigns the variable m the smallest of the variables a and b. That means that if the condition a < b is 
true, given m value of a, otherwise the value of b. You can also write the same with the question operator
m = a < b ? a : b;
and the meaning is exactly the same. The question operator starts with a condition and if it is true, the 
value of the expression is the value of the expression (variable) after the question mark and if not the 
value of the expression is the value after the colon. In this example the statement
return a < b ? b : a;
will return the largest of the variables a and b. The question operator is often an alternative to an if 
statement, but it is of course the difference that it is an operator and thus can be included in expression 
as in the above return statement. As mentioned, there are some who think that the operator\u2019s is hard to 
read, but with practice it is as natural as all other operators.
Methods return values
The methods described in the previous example, all have a return type which is either int or double. 
When a method has a return type, it has a value after it has been executed. The method can therefore 
be part of an expression in the same way as a variable. For example one can write the following:
double x = Max(3.14, 1.41);
which means that the return value of method Max() is stored in the variable x. Once a method has a 
return type, it ends with a return statement, which determines the return value \u2013 often as a result of 
an expression. There is not much to say concerning methods return types, except that the return type 
can be anything \u2013 including reference types \u2013 and that a method can have only one return value. As an 
example is shown a simple input method that returns a string:
static string Indtast(string text)
{
Console.Write(text + &quot;: &quot;);
return Console.ReadLine();
}
As a last remark concerning return types it must also include void that is not a type, but simply a term 
for a method that has no return value. Although the method is void, it may well have an empty return 
statement, which did not gives the method a value, but only terminate the method.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
91 
Methods
Properties
A property is, as mentioned earlier really just a method with a different syntax and, in principle, a property 
can do anything. The idea of a property is, however, that it read (returns) the value of an instance variable 
and possible changes its value. In addition, you should use the convention that the name of a property 
is the variable\u2019s name written in uppercase.
Exam25
A point
This example illustrates a simple class representing a point in a coordinate system.
How to
class Point
{
private int x;
private int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
92 
Methods
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public override string ToString()
{
return string.Format(&quot;({0},{1})&quot;, x, y);
}
}
Explanation
The coordinates are private, and for that you can\u2019t access them from outside the class. To do so you must 
define get properties. If it also should be possible to modify an object\u2019s coordinates, the class must also 
define set properties, for example
set { x = value; }
The meaning is that you can change the value of the variable x, but the syntax is a bit special, corresponding 
to the value to be assigned to x is represented by the reserved word value. The result is that, in a program 
you can be write as follows:
Point p = new Point(2, 3);
p.X = 5;
where the x-coordinate gets a different value.
When a class as above offers both get and set properties, and set properties do nothing more than to 
assign the value of value, so there is no difference to simply define both variables as public. It is advisable, 
nevertheless, to comply with the definition of variables private and then the necessary properties, since, 
as mentioned that means, that it is up to the programmer, for unlocking the protection. There will often 
be assigned limitations to set properties in a class. If,