Syntax In C#
Syntax In C# is a bit the same with Java depending on the syntax that derives from C and C++. C# program is in need of powerful structure when it comes to its object-oriented nature in terms of classes. Wherein, the statement needs to be exact.
A statement is a basic implementation of C# Syntax program. This statement can be declared as variable, execute an event by calling a method, create a method and many more, and it ends with semicolon.
Examples:
namespace myFirstCsharp
{
public partial class Form1 : Form
{
public Form1()
{
//This method is for the designer support.
//Reminder: do not modify the content of this method with the code editor.
InitializeComponent();
}
//creating a method
private void HelloWorld()
{
string msg;// declaring varible
msg = "Hello world!"; //storing value in a variable
MessageBox.Show(msg); //desplaying value of a variable
}
private void button1_Click(object sender, EventArgs e)
{
HelloWorld(); // calling a method
}
}
}
output:
A statement between { } braces is the body of the method. These braces serve as the fence of the codes in the method. This will help you identify what is the process or event you’re going to execute.
private void Calculate() { //start of the method //variables are accessable any where inside the method. int input1 = 3; int input2 = 2; if (input1 > 2) { //variable is accessable here only. int tot; tot = input1 - input2; MessageBox.Show(tot.ToString()); } }//end of the method private void button1_Click(object sender, EventArgs e) { Calculate(); // calling a method }