C
C#16mo ago
Ronnie

✅ multiple if statements

using System;
using System.Collections.Generic;


namespace CSharp
{
class Program
{

static void MyMethod(int var1, int var2, int var3)
{
if ((var1 > var2) && (var1 > var3)){
Console.WriteLine($"{var1} is the largest value");
}else if ((var2 > var1) && (var2 > var3)){
Console.WriteLine($"{var2} is the largest value");
}else if ((var3 > var1) && (var3 > var2)){
Console.WriteLine($"{var3} is the largest value");
}
}


static void Main(string[] args)
{
/*int[,] arr = { {1, 2, 3}, {4, 5, 6} };

for (int i = 0; i < arr.GetLength(0); i++){
for (int j = 0; j < arr.GetLength(1); j++){
Console.WriteLine(arr[i, j]);
}
}*/

MyMethod(5, 7, 9);
}
}
}
using System;
using System.Collections.Generic;


namespace CSharp
{
class Program
{

static void MyMethod(int var1, int var2, int var3)
{
if ((var1 > var2) && (var1 > var3)){
Console.WriteLine($"{var1} is the largest value");
}else if ((var2 > var1) && (var2 > var3)){
Console.WriteLine($"{var2} is the largest value");
}else if ((var3 > var1) && (var3 > var2)){
Console.WriteLine($"{var3} is the largest value");
}
}


static void Main(string[] args)
{
/*int[,] arr = { {1, 2, 3}, {4, 5, 6} };

for (int i = 0; i < arr.GetLength(0); i++){
for (int j = 0; j < arr.GetLength(1); j++){
Console.WriteLine(arr[i, j]);
}
}*/

MyMethod(5, 7, 9);
}
}
}
is there any way to make the MyMethod() function shorter?
6 Replies
ero
ero16mo ago
var max = Math.Max(var1, Math.Max(var2, var3));
Console.WriteLine($"{max} is the largest value");
var max = Math.Max(var1, Math.Max(var2, var3));
Console.WriteLine($"{max} is the largest value");
Ronnie
Ronnie16mo ago
oh.
ero
ero16mo ago
or
void MyMethod(params int[] vars)
{
var max = vars.Max();
Console.WriteLine($"{max} is the largest value");
}
void MyMethod(params int[] vars)
{
var max = vars.Max();
Console.WriteLine($"{max} is the largest value");
}
but don't worry about that yet if you're a beginner ;)
Ronnie
Ronnie16mo ago
oh yeah, thanks. also one quick question about curly braces formatting. Is it better to have them on a separate line or on the same line your starting the conditional/method? i just cant decide @Ero
ero
ero16mo ago
the official convention is new line
Ronnie
Ronnie16mo ago
ahh oh thanks