c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I830 materiais10.236 seguidores
Pré-visualização50 páginas
choices made \u2013 you 
must explain how the algorithms are used and how they work. Are there solutions that are difficult to 
comprehend, then you should add explanatory comments. It is also wise to always document the variables 
and what they used for \u2013 at least instance variables. It can also be a good idea to add a comment, telling 
about modification of the code, when changes are made, to whom and why, and of course what\u2019s changed.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
185 
Comments
You should special be consistent about the auto-generated comments and include them \u2013 at least for 
all public program elements. It is especially important for class libraries, which often must be used by 
anyone other than the programmer who wrote the classes. It can be hard to write that kind of evidence 
simply because it can be hard to find something to write (many methods and properties are obvious and 
self explanatory) and you often think that you do not have anything to write. Yet it is a place where you 
should be consistent and include these comments. One should be aware that this kind of comments is 
intended for those who must use the code, and not to those who need to maintain the code.
The program\u2019s code readability is extremely important, and you can even go so far that the code that is 
not easy to read is worthless. However, you can do many things to make the code readable than writing 
comments and including the following few guidelines:
\u2022	 a block starts on a new line containing only the character {
\u2022	 from the next line makes an indentation of two characters
\u2022	 when a block ends repealed the indentation, and you move two characters to the left
\u2022	 a block always end on a new line containing only the character }
\u2022	 add always a space on either side of an operator
\u2022	 a variable name always starts with a lowercase letter
\u2022	 a name of a method, property, and a user-defined type always starts with a capital letter
We will turn your CV into 
an opportunity of a lifetime
Do you like cars? Would you like to be a part of a successful brand?
We will appreciate and reward both your enthusiasm and talent.
Send us your CV. You will be surprised where it can take you.
Send us your CV on
www.employerforlife.com
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
186 
Comments
\u2022	 be consistent in capitalization
\u2022	 use good and explanatory names, but not for long names \u2013 they are hard to read
\u2022	 use blank lines where you think it increases the readability
And so be consistent and have a style. Guidelines are good, but there will always be places where you 
may depart from them, but if you do it consistently, it is excellent. The above guidelines are to make the 
code self-documenting, and one can say that the comments should be used if the code can\u2019t explain itself.
One can hardly say enough about the importance of writing readable program code, but in terms of 
comments, you can also go too far. Generally I feel that a comment inside a method makes the code 
harder to read. It can be difficult to see what is program code and what\u2019s comments \u2013 the comments 
shadows the code. Comments inside the code I include only where I think they are absolutely necessary \u2013 
and it is certainly often the case. The conclusion is that documentation is important, but exaggeration 
may have the opposite effect.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
187 
Extension methods
23 Extension methods
If you want to extend a class with new methods, the approach is to write a derived class that adds the 
new methods. It is still the \u201cright\u201d strategy, but it is not always possible, for example if the class is sealed \u2013 
it is a class that you can\u2019t inherit. One can however achieve the same thing with an extension method.
Consider the following class that defines three static methods to integers:
public static class Integer
{
public static long DiffSum(this int n)
{
return (n + 1L) * n / 2;
}
public static int Add(this int n, params int[] t)
{
int s = n;
for (int i = 0; i < t.Length; ++i) s += t[i];
return s;
}
public static bool IsPrim(this int n)
{
if (n == 2 || n == 3 || n == 5 || n == 7) return true;
if (n < 11 || n % 2 == 0) return false;
for (int k = 3, m = (int)Math.Sqrt(n) + 1; k <= m; k += 2)
if (n % k == 0) return false;
return true;
}
}
The first determines the sum of the numbers 1 + 2 + 3 + 4 + \u2026 + N. This can be done with a loop, but 
you can also use a formula as has been done above. The second method returns the sum of a series of 
integers, while the latter method tests whether an integer is a prime. Since all the methods are static, 
they may be carried out as follows:
Console.WriteLine(Integer.Add(2, 3, 5, 7, 11, 13, 17, 19));
Console.WriteLine(Integer.DiffSum(100));
Console.WriteLine(Integer.IsPrim(97));
which is not strange. You should however note that the class is static, and that the first parameter to 
each of the three methods is of the type int, and the declarations of these parameters are prefixed with 
the word this. It is the two factors that make that make the methods to extension methods. This means 
that methods can be performed as if they were instance methods defined for type int:
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
188 
Extension methods
int a = 2;
Console.WriteLine(a.Add(3, 5, 7, 11, 13, 17, 19));
int b = 100;
Console.WriteLine(b.DiffSum());
int c = 97;
Console.WriteLine(c.IsPrim());
and not only that \u2013 the methods are known to Intellisense in Visual Studio.
Apparently the type int is extended with new methods, but it is obviously not the case. An extension method 
is a usual static method, and it should be written in the same way as other static methods and can\u2019t refer 
for instance members of the class to which it is an extension. There are only talking about that with the 
word this in front of the first parameter it allows to use a method with same syntax as if it were an instance 
method. If you compare the above applications of the methods in the class Integer, it is clear that it is only 
a question of how to specify the first parameter \u2013 as a normal value or by using dot notation.
Extension methods have their uses, and is as such used by Microsoft a great in relation to LINQ.
In the previous section I showed a class String. It was a static class with static methods, where the first 
parameter in all methods has the type string (that is the type System.String). It is therefore extremely 
simple to modify these methods to extension methods for the String class \u2013 it\u2019s just adding the word this 
in front of the first parameter to all methods:
© Agilent Technologies, Inc. 2012 u.s. 1-800-829-4444 canada: 1-877-894-4414
Budget-Friendly. Knowledge-Rich.
The Agilent In\ufb01niiVision X-Series and 
1000 Series offer affordable oscilloscopes 
for your labs. Plus resources such as 
lab guides, experiments, and more, 
to help enrich your curriculum 
and make your job easier.
See what Agilent can do for you.
www.agilent.com/\ufb01nd/EducationKit
Scan for free 
Agilent iPhone 
Apps or visit 
qrs.ly/po2Opli
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
189 
Extension methods
public static class Str
{
public static string Cut(this string text, int length)
{
if (text.Length > length) return text.Substring(0, length);
return text;
}
public	static	string	FillRight(this	string	text,	int	length,	char	fill)
{
if (text.Length >= length) return text;
StringBuilder builder = new StringBuilder(text, length);
while	(builder.Length	<	length)