Ambiguous Methods
[code language=”csharp”] using static System.Console;class AmbiguousMethods
{
static void Main()
{
int iNum = 20;
double dNum = 4.5;
SimpleMethod(iNum, dNum); // calls first version
SimpleMethod(dNum, iNum); // calls second version
SimpleMethod(iNum, iNum); // error! Call is ambiguous.
}
private static void SimpleMethod(int i, double d)
{
WriteLine("Method receives int and double");
}
private static void SimpleMethod(double d, int i)
{
WriteLine("Method receives double and int");
}
}
[/code]
Border Demo 1
[code language=”csharp”] using static System.Console;class BorderDemo1
{
static void Main()
{
DisplayWithBorder("Ed");
DisplayWithBorder("Theodore");
DisplayWithBorder("Jennifer Ann");
}
private static void DisplayWithBorder(string word)
{
const int EXTRA_STARS = 4;
const string SYMBOL = "*";
int size = word.Length + EXTRA_STARS;
int x;
for(x = 0; x < size; ++x)
Write(SYMBOL);
WriteLine();
WriteLine(SYMBOL + " " + word + " " + SYMBOL);
for(x = 0; x < size; ++x)
Write(SYMBOL);
WriteLine("\n\n");
}
}
[/code]
Border Demo 2
[code language=”csharp”] using static System.Console;class BorderDemo2
{
static void Main()
{
DisplayWithBorder("Ed");
DisplayWithBorder(3);
DisplayWithBorder(456);
DisplayWithBorder(897654);
DisplayWithBorder("Veronica");
}
private static void DisplayWithBorder(string word)
{
const int EXTRA_STARS = 4;
const string SYMBOL = "*";
int size = word.Length + EXTRA_STARS;
int x;
for(x = 0; x < size; ++x)
Write(SYMBOL);
WriteLine();
WriteLine(SYMBOL + " " + word + " " + SYMBOL);
for(x = 0; x < size; ++x) Write(SYMBOL); WriteLine("\n\n"); } private static void DisplayWithBorder(int number) { const int EXTRA_STARS = 4; const string SYMBOL = "*"; int size = EXTRA_STARS + 1; int leftOver = number; int x; while(leftOver >= 10)
{
leftOver = leftOver / 10;
++size;
}
for(x = 0; x < size; ++x)
Write(SYMBOL);
WriteLine();
WriteLine(SYMBOL + " " + number + " " + SYMBOL);
for(x = 0; x < size; ++x)
Write(SYMBOL);
WriteLine("\n\n");
}
}
[/code]
Conversion with TryParse
[code language=”csharp”] using static System.Console;class ConversionWithTryParse
{
static void Main()
{
string entryString;
int score;
Write("Enter your test score >> ");
entryString = ReadLine();
int.TryParse(entryString, out score);
WriteLine("You entered {0}", score);
}
}
[/code]
Input Method Demo
[code language=”csharp”] using System;using static System.Console;
class InputMethodDemo
{
static void Main()
{
int first, second;
InputMethod(out first, out second); // notice use of out
WriteLine("After InputMethod first is {0}", first);
WriteLine("and second is {0}", second);
}
private static void InputMethod(out int one, out int two)
// notice use of out
{
string s1, s2;
Write("Enter first integer ");
s1 = ReadLine();
Write("Enter second integer ");
s2 = ReadLine();
one = Convert.ToInt32(s1);
two = Convert.ToInt32(s2);
}
}
[/code]
Optional Parameter Demo
[code language=”csharp”] using static System.Console;class OptionalParameterDemo
{
static void Main()
{
Write("Using 2 arguments: ");
DisplaySize(4, 6);
Write("Using 3 arguments: ");
DisplaySize(4, 6, 8);
}
private static void DisplaySize(int length, int width, int height = 1)
{
int area = length * width * height;
WriteLine("Size is {0}", area);
}
}
[/code]
Params Demo
[code language=”csharp”] using static System.Console;class ParamsDemo
{
static void Main()
{
string[] names = {"Mark", "Paulette", "Carol", "James"};
DisplayStrings("Ginger");
DisplayStrings("George", "Maria", "Thomas");
DisplayStrings(names);
}
private static void DisplayStrings(params string[] people)
{
foreach(string person in people)
Write("{0} ", person);
WriteLine("\n—————-");
}
}
[/code]
Parameter Demo 1
[code language=”csharp”] using static System.Console;class ParameterDemo1
{
static void Main()
{
int x = 4;
WriteLine("In Main x is {0}", x);
DisplayValueParameter(x);
WriteLine("In Main x is {0}", x);
}
private static void DisplayValueParameter(int x)
{
x = 777;
WriteLine("In method, x is {0}", x);
}
}
[/code]
Parameter Demo 2
[code language=”csharp”] using static System.Console;public class ParameterDemo2
{
static void Main()
{
int x = 4;
WriteLine("In Main x is {0}", x);
DisplayReferenceParameter(ref x);
WriteLine("In Main x is {0}", x);
}
private static void DisplayReferenceParameter(ref int number)
{
number = 888;
WriteLine("In method, number is {0}", number);
}
}
[/code]