1

I'm totally new to .NET and I'm trying to get to know C# by running the codes I encounter in the books I follow..

I'm building a simple WPF app with a button, which should print out the hypoteneuse. My question is; In the bellow code example from the book, there is these two namespaces (Syncfusion and NamespaceDemo). Do I have to include them both? What is a better way to use these code? Secondly, When creating a new WPF file and a button for the application, it automatically generates this piece of code:

 public MainWindow()
        {
            InitializeComponent();
        }

I know that the MainWindow() is for the design that will include the button. How does it differs from the Main() function in a simple C# console application? I would appreciate a clear explaination about my confusion with how this different things has to be structured properly. Do I need a Main()?

This is the code from book:

using static System.Math; 
namespace Syncfusion  
{ 
public class Calc 
{ 
public static double Pythagorean(double a, double b) 
{ double cSquared = Pow(a, 2) + Pow(b, 2); 
return Sqrt(cSquared); } 
} 
}


using Syncfusion; 
using System; 
using Crypto = System.Security.Cryptography; 

namespace NamespaceDemo 
{ 
class Program 
{ 
static void Main() 
{ 
double hypotenuse = Calc.Pythagorean(2, 3); 
Console.WriteLine("Hypotenuse: " + hypotenuse); 
Crypto.AesManaged aes = new Crypto.AesManaged(); 
Console.ReadKey();
 } 
} 
}

This is my implementation, which unfortunately doesn't work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Math;


namespace Syncfusion
{
    public class Calc
    {
        public static double Pythagorean(double a, double b)
        {
            double cSquared = Pow(a, 2) + Pow(b, 3);
            return Sqrt(cSquared);
        }
    }


    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void Button_Click(object sender, RoutedEventArgs e)
        {
            double hypotenuse = Calc.Pythagorean(2, 4);
            MessageBox.Show("Hypotenuse: " + hypotenuse);
        }
    }
}
  • If you don't understand a line of code, you ideally should not be using it. AES is not needed for your form or math function – OneCricketeer Jun 30 '18 at 13:30
  • Great hints. Thanks. –  Jun 30 '18 at 13:48
  • "My question is; In the bellow code example from the book, there is these two namespaces (Syncfusion and NamespaceDemo). Do I have to include them both?" The Syncfusion is only necessary if you're using one of the their features or components. The other namespace is probably for your book sample code. – nocturns2 Jun 30 '18 at 13:54

1 Answers1

0

All C# programs require a static Main methods as an entry point. MainWindow is a class, not an entry point.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Thanks. So I have to place the button_Clicked() in the Main()? –  Jun 30 '18 at 13:32
  • 1
    @user9419320 not exactly. it doesn't look like you need wpf at all since you are using `Console` – Daniel A. White Jun 30 '18 at 13:34
  • I'm not using console. I'm trying wpf. but the code from the book is for the console. I'm trying to adopt it to the wpf. –  Jun 30 '18 at 13:36