<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C# Help &#187; Methods</title>
	<atom:link href="http://www.csharphelp.com/tag/methods/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharphelp.com</link>
	<description>C# Tutorial and Guides</description>
	<lastBuildDate>Tue, 07 Feb 2012 01:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C# Methods</title>
		<link>http://www.csharphelp.com/2007/03/c-methods/</link>
		<comments>http://www.csharphelp.com/2007/03/c-methods/#comments</comments>
		<pubDate>Wed, 07 Mar 2007 04:01:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Methods]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com.php5-3.dfw1-2.websitetestlink.com/?p=475</guid>
		<description><![CDATA[Introduction to C# Methods : Just in the beginning I like to remind all of you that Computer programs exist to solve a problems and there are methods for solving those problems. That&#39;s how I can explain the meaning of C# methods. All C# programs constructed from a number of classes and those classes contain [...]]]></description>
			<content:encoded><![CDATA[<p><span class="smallblack"><b><span style="color:#0000ff;">Introduction to C# Methods :</span></b></span></p>
<p><span class="smallblack">Just in the beginning I like to remind all of you that Computer programs exist to solve a problems <b>and there are methods for solving those problems</b>.</span></p>
<p><span class="smallblack">That&#39;s how I can explain the meaning of C# methods. All C# programs constructed from a number of classes and those classes contain methods that solve the problems for the program. So a methods is a kind of building blocks that solve a small problem.</span></p>
<p><span class="smallblack">For example you have a problem in your program. You need a method to convert a string to integer array for some reason. You can use the following method :</span></p>
<p><span class="smallblack"><span style="color:#0000ff;font-size:x-small;"></span></span></p>
<p><span class="smallblack"><span style="color:#0000ff;font-size:x-small;"><span style="font-family:Courier New;">private</span></span><span style="font-family:Courier New;"><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">int</span><span style="font-size:x-small;">[] ToIntArray(</span><span style="color:#0000ff;font-size:x-small;">string</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;"> s)</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">{</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">&nbsp;&nbsp;&nbsp; int</span><span style="font-size:x-small;"><span style="font-family:Courier New;"> o = s.Length;</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;">&nbsp;&nbsp;&nbsp; int</span><span style="font-size:x-small;">[] iArray =</span><span style="color:#0000ff;font-size:x-small;">new</span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;">[o];</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;">&nbsp;&nbsp;&nbsp; for</span><span style="font-size:x-small;">(</span><span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;"> x = 0; x &lt; s.Length; x++)</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">&nbsp;&nbsp;&nbsp; {</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iArray[x] = Convert.ToInt32(s.Substring(x,1));</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">&nbsp;&nbsp;&nbsp; }</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">&nbsp;&nbsp;&nbsp; return</span><span style="font-size:x-small;"><span style="font-family:Courier New;"> iArray;</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">}</span></span></span></p>
<p><span class="smallblack"></span></p>
<p><span class="smallblack"><span style="font-family:newtimes;">So with this method we solve the problem we have here ( if we have a string like &quot;911&quot; this method will convert it to integer array [9,1,1] ). We will not talk about the code written inside the method now but we will focus on introducing you to methods and later we will take a lot of examples. Just note that we use the .NET Framework Class Library (FCL) methods (like ToInt32(), Substring()) inside our methods to create a new methods. That&#39;s make our job as a programmers much easier.</span></span></p>
<p><span class="smallblack"><span style="font-family:newtimes;">When you declare a variable in your method then it&#39;s a local variable to that method and no other method can know anything about that variable.</span></span></p>
<p><span class="smallblack"><span style="font-family:newtimes;">Methods also very useful if you will use a block of code many times in your program so instead of writing the same block of code many times in the program we can put that code in a method and call it whenever we need it to do the job.</span></span></p>
<p><span class="smallblack"><b><span style="font-family:newtimes;color:#0000ff;">How we write C# methods ?</span></b></span></p>
<p><span class="smallblack"><span style="font-family:newtimes;">After reading this section you will be able to write methods and understand the concept of (Parameters, Return Value)</span></span></p>
<p><span class="smallblack"><span style="font-family:newtimes;">We have a line of code and we need to put it inside a method so we understand how we can write C# methods.</span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;font-size:x-small;">
<p>Console.WriteLine(Math.Sqrt(9));</p>
<p></span><span style="font-family:Times New Roman;">
<p>This line of code use the Sqrt method of the class Math to get the square root of 9 . We need to create a method to write to the console screen the square root just when we use it (<b>when we call it, Invoke it)</b>. Let&#39;s take a look at this method :</p>
<p></span><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;"></span>
<p><span style="color:#0000ff;font-size:x-small;">static</span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">void</span><span style="font-size:x-small;"> SqrtToConsole(</span><span style="color:#0000ff;font-size:x-small;">double</span><span style="font-size:x-small;"> x)</span></p>
<p><span style="font-size:x-small;">{</span></p>
<p><span style="font-size:x-small;">&nbsp;&nbsp;&nbsp; Console.WriteLine(Math.Sqrt(x));</span></p>
<p><span style="font-size:x-small;">}</span></p>
<p></span><span style="font-family:Times New Roman;"></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">to understand this method we need to explain the concept of <b>parameters</b><i>.</i></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><span style="color:#0000ff;"><b>Parameters and parameters value :</b></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">A lot of times the method you call will need some information from you to complete its task. For example the ToIntArray() method in the article take one parameter as a string :</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><span style="color:#0000ff;font-size:x-small;"></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><span style="color:#0000ff;font-size:x-small;"><span style="font-family:Courier New;">private</span></span><span style="font-family:Courier New;"><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">int</span><span style="font-size:x-small;">[] ToIntArray(</span><span style="color:#0000ff;font-size:x-small;">string</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;"> s)&lt;&#8212;&#8212;&#8212;&#8212;&#8212; This is called a parameter</span></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">So this place where we will put the string into called a <b>parameter</b>. We just tell the method &quot;You have one place (a parameter) where can the user put a value here (a parameter value) and you need this value to finish your task&quot;. So when you create the method you specify the parameters that the method will use and when you call it you will specify the parameters value for the parameters. <b>parameters value<</p>
<p>/b> are<br />
the values that you specify for your parameters when you call the function. Every parameters in your method has a data type exactly like variables and you must specify it when you create the method. When you call the method you must specify the parameters value of the same data type or to a type that can be converted to that type. The parameters of any method not accessible outside the method because it&#39;s considered as local variables for that method.</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">Consider the following example :</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;"></span>
<p><span style="color:#0000ff;font-size:x-small;">static</span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">void</span><span style="font-size:x-small;"> SqrtToConsole(</span><span style="color:#0000ff;font-size:x-small;">double</span><span style="font-size:x-small;"> x)</span><span style="font-family:Times New Roman;font-size:x-small;"><span style="font-family:Courier New;">&lt;&#8212;&#8212;&#8212;&#8212;&#8212; This is called a parameter</span></span></p>
<p><span style="font-size:x-small;">{</span></p>
<p><span style="font-size:x-small;">&nbsp;&nbsp;&nbsp; Console.WriteLine(Math.Sqrt(x));</span></p>
<p><span style="font-size:x-small;">}</span></p>
<p></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">We just created a method called SqrtToConsole that take only one parameter. Now we need to call the method ( I mean we need to use the method).</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><span style="font-family:Courier New;font-size:x-small;">SqrtToConsole(9)</span><span style="font-family:Courier New;"><span style="font-family:Courier New;font-size:x-small;">&lt;&#8212;&#8212;&#8212;&#8212;&#8212; This is called a parameter value</span></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><b>We called the method <span style="font-family:Courier New;font-size:x-small;">SqrtToConsole </span>with 9 as a parameter value for the parameter x</b>.</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">I think after you understand the concept of Parameters we can explain the code written in the method <span style="font-family:Courier New;font-size:x-small;">SqrtToConsole</span>.</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;"><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;"></span>
<p><span style="color:#0000ff;font-size:x-small;">static</span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">void</span><span style="font-size:x-small;"> SqrtToConsole(</span><span style="color:#0000ff;font-size:x-small;">double</span><span style="font-size:x-small;"> x)</span></p>
<p><span style="font-size:x-small;">{</span></p>
<p><span style="font-size:x-small;">&nbsp;&nbsp;&nbsp; Console.WriteLine(Math.Sqrt(x));</span></p>
<p><span style="font-size:x-small;">}</span></p>
<p></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">We just created a method called <span style="font-family:Courier New;font-size:x-small;">SqrtToConsole </span>with one parameter called x of double data type. We use 2 FCL functions here (<span style="font-family:Courier New;font-size:x-small;">WriteLine(),Sqrt())</span><span style="font-family:Courier New;"></span>to complete the task. </span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">The Console.WriteLine() method take one parameter value to write it to the console. Note that the parameter value of this parameter can be an expression . There are 18 overloaded version of Console.WriteLine() method and you can check them in Visual Studio.NET Documentation. Some of them take a parameter value as a string, int, double, object and other data types.</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">The Math.Sqrt() method take also one parameter value to get its job done. Here we just create our method and inside it we call the Console.WriteLine() with a parameter value as the result of the following method call<span style="font-family:Courier New;font-size:x-small;">Math.Sqrt(x)</span>.</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">Note : Don&#39;t forget that x is the parameter of the<span style="font-family:Courier New;font-size:x-small;">SqrtToConsole() </span>method and we just use it in the <span style="font-family:Courier New;font-size:x-small;">Math.Sqrt(x)</span> method so that we tell the compiler to take the parameter value of<span style="font-family:Courier New;font-size:x-small;">SqrtToConsole()</span></span><span style="font-family:new;"> method and give it to the </span><span style="font-family:Courier New;font-size:x-small;">Math.Sqrt()</span><span style="font-family:new;">so the result will write to the console application.</span></span></p>
<p><span class="smallblack"><b><span style="font-family:new;color:#0000ff;">Return Value :</span></b></span></p>
<p><span class="smallblack"><span style="font-family:new;">Let&#39;s look at the </span><span style="font-family:Times New Roman;">Math.Sqrt() again please. This method takes a parameter of double data type and </span>Returns the square root of that parameter so this is the <b>return value</b> that we are talking about here in C# methods.</span></p>
<p><span class="smallblack">Consider the following method :</span></p>
<p><span class="smallblack"><span style="color:#0000ff;font-size:x-small;"></span></span></p>
<p><span class="smallblack"><span style="color:#0000ff;font-size:x-small;"><span style="font-family:Courier New;">private</span></span><span style="font-family:Courier New;"><span style="font-size:x-small;"></span><span style="color:#0000ff;font-size:x-small;">int</span><span style="font-size:x-small;"> AddIntegers(</span><span style="color:#0000ff;font-size:x-small;">int</span><span style="font-size:x-small;"> x, </span><span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;"> y)</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">{</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">&nbsp;&nbsp;&nbsp; return</span><span style="font-size:x-small;"><span style="font-family:Courier New;"> x + y ;</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">}</span></span></span></p>
<p><span class="smallblack"></span></p>
<p><span class="smallblack"><span style="font-family:ne;">Here the AddIntegers() method takes 2 integer numbers and return the result as integer too.</span></span></p>
<p><span class="smallblack"><span style="font-family:ne;">The return value must have a type so here in our method the return value is of type int and we must write int keyword before the method name to specify the return type. Also we use the </span><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">return</span><span style="font-family:ne;"> keyword as the end of our method to exit the method and return the value to the method which called the AddIntegers() ( I will explain how we can call method from other method later ) . So when you use the </span><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">return</span><span style="font-family:ne;"> keyword you tell the compiler to end the method and return a value of the specified type to the calling code. If your method does not return a value then you must use the </span><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">void</span><span style="font-family:ne;"> keyword in the same place as you do with the return value type ( before the met</p>
<p>hod name).</span></span></p>
<p><span class="smallblack">You must understand something called Method Signature or Method Definition. The group of method name, parameter list, return value type specify the method signature or method definition. For example the method <span style="font-family:ne;">AddIntegers() signature is : </span></span></p>
<p><span class="smallblack"><span style="font-family:ne;">1- The return data type </span><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">(int</span><span style="font-family:ne;">)</span></span></p>
<p><span class="smallblack"><span style="font-family:ne;">2- The method name ( </span><span style="font-family:Courier New;color:#0000ff;font-size:x-small;">AddIntegers</span><span style="font-family:ne;"> )</span></span></p>
<p><span class="smallblack"><span style="font-family:ne;">3- The parameter list ( </span><span style="font-family:Courier New;"><span style="font-size:x-small;">(</span><span style="color:#0000ff;font-size:x-small;">int</span><span style="font-size:x-small;"> x, </span><span style="color:#0000ff;font-size:x-small;">int</span></span><span style="font-family:Courier New;font-size:x-small;"> y) </span><span style="font-family:Times New Roman;">)</span></span></p>
<p><span class="smallblack"><b><span style="font-family:Times New Roman;color:#0000ff;">How we call a method ?</span></b></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">As you might know that the Main method is the entry point of any C# program so it&#39;s the first method that the compiler will invoke. So any method we created or any method we used it from FCL will be called from the Main method. Consider the following example :</span></span></p>
<p><span class="smallblack"><span style="font-family:Times New Roman;">We have 2 methods as following :</span></span></p>
<p><span class="smallblack"><span style="color:#0000ff;font-size:x-small;"></span></span></p>
<p><span class="smallblack"><span style="color:#0000ff;font-size:x-small;"><span style="font-family:Courier New;">private</span></span><span style="font-family:Courier New;"><span style="font-size:x-small;"></span><span style="color:#0000ff;font-size:x-small;">void</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;"> Parent()</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">{</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;color:#008000;font-size:x-small;">&nbsp;&nbsp;&nbsp; // We don&#39;t need any code here</span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">}</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;">private</span><span style="font-size:x-small;"></span><span style="color:#0000ff;font-size:x-small;">void</span></span><span style="font-size:x-small;"><span style="font-family:Courier New;"> Son(<span style="color:#0000ff;">int</span> x)</span></span></span></p>
<p><span class="smallblack"><span style="font-size:x-small;"><span style="font-family:Courier New;">{</span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;color:#008000;font-size:x-small;">&nbsp;&nbsp;&nbsp; // We don&#39;t need any code here</span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;font-size:x-small;">
<p>}</p>
<p></span><span style="font-family:ne;">
<p>We need to call this methods from our Main method. All what we need to do like that </p>
<p></span><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;"></span></span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;"><span style="color:#0000ff;font-size:x-small;">static</span><span style="font-size:x-small;"> </span><span style="color:#0000ff;font-size:x-small;">void</span><span style="font-size:x-small;"> Main(</span><span style="color:#0000ff;font-size:x-small;">string</span></span><span style="font-family:Courier New;font-size:x-small;">[] args)</span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;font-size:x-small;">{</span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;font-size:x-small;">&nbsp;&nbsp;&nbsp; Parent();</span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;font-size:x-small;">&nbsp;&nbsp;&nbsp; Son(23);</span></span></p>
<p><span class="smallblack"><span style="font-family:Courier New;font-size:x-small;">}&nbsp;&nbsp;&nbsp; </span></span></p>
<p><span class="smallblack"><span style="font-family:ne;">
<p>We called the 2 methods simply by typing the name of the method with 2 empty parenthesis if the method&#39;s parameter list is empty or the with parameters values inside the parenthesis if the method&#39;s parameter list contains parameters.</p>
<p>Note : a user-defined method can call other methods like another user-defined method or FCL method.</p>
<p>So far I think I have introduced C# methods and in my next article I will talk complete our discussion about methods and introduce some advanced concepts. I hope that you will find my articles useful.</p>
<p><span style="color:#ff0000;"><b>About the author :</b></span></p>
<p>Michael Youssef is 20 years old and he&#39;s working with Microsoft technologies since he was 17. He is working with VB, VB.NET, C#,ASP, ASP. NET, XML Web services, COM+, SQL Server 2000, Windows 2000 Server, Active Directory, Networks design. He is MCSD.NET, MCDBA, MCSE, MCSA and he&#39;s working now as a .NET Trainer in Egypt. Michael can be reached at <a href="mailto:michaellabibat@hotmail.com">michaellabibat@hotmail.com</a> .</p>
<p></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2007/03/c-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

