C#/c#

Advertisement


Question
QUESTION: My friends told me that it is fun learning c#..
I too thought of having a try..
I have installed Visual studio 2005 too.
please tell me where should I type the program and execute it.. Also, can you give me a sample program for getting two numbers from the user, summing it and printing the result.. I hope that this would be a good start for me to learns it. . is it almost like c or c++??

ANSWER: In order to answer your question, I just did what you requested and wrote down notes every step of the way. Those notes are pasted below. Let me know if you need clarification for anything stated. Here goes:

Open Visual Studio
File -> New -> Project
Visual C# -> Windows -> Windows Application
Name project "Addition_Test".
Your project should now be created.
In the menu at the top of the screen, click "View" -> "Toolbox".
Create two text boxes (double click the TextBox item, or drag it on to your form). You should find the TextBox item under the "Common Controls" section of the Toolbox.
Position the text boxes where you like.
Rename the first text box to "txtNumber1"
  -Right click on it and select "Properties".
  -Change its "(Name)" property to "txtNumber1".
Rename the second text box to "txtNumber2"
  -Right click on it and select "Properties".
  -Change its "(Name)" property to "txtNumber2".
Go back to the toolbox and create a button.
Name the button "btnAdd" and change its "Text" property to "Add".
Double click the "btnAdd" button so that an event handler is created for its click event.
Add the following code to the click event handler you just created:
         int num1 = int.Parse(this.txtNumber1.Text);
         int num2 = int.Parse(this.txtNumber2.Text);
         int result = num1 + num2;
         MessageBox.Show(result.ToString());
In the menu at the top of the screen, click "Debug" -> "Start Debugging".
Enter some numbers in the text boxes and click the "Add" button. The result will pop up in a new window.
Click the close button on th result window and then on your main program window. This will close the program and stop it from running.
If you have any problems stopping a program you made from running, go to "Debug" -> "Stop Debugging" and it will force your program to stop.
If everything did not go according to plan, compare your results to mine. There are two views for your form. One is a design view and one is a code view. When you are in design view, you can right click your form and select "View Code". When you are in code view, you can right click the code and select "View Designer". Somewhere on the screen, you should see your "Solution Explorer". This is where you can see a list of all the files in your project. If you do not have your form open, you would double click your form in the "Solution Explorer" and it would open. Now, the results I got were that I could see two text boxes and one button when in design view. When in code view, I saw the following code (much of it auto-generated for me):


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Addition_Test
{
   public partial class Form1 : Form
   {
       public Form1()
       {
         InitializeComponent();
       }

       private void btnAdd_Click(object sender, EventArgs e)
       {
         int num1 = int.Parse(this.txtNumber1.Text);
         int num2 = int.Parse(this.txtNumber2.Text);
         int result = num1 + num2;
         MessageBox.Show(result.ToString());
       }
   }
}

Good luck with your new endeavor.

-Nick

---------- FOLLOW-UP ----------

QUESTION: Thank you for your immediate response. But I think whatever you gave is all VC#. But what I want is C# (for console applications).

Answer
Don't quote me on this, but I think what you are looking for and what I gave you are both Visual C#. I looked up Visual C# on Wikipedia and it said that Visual C# is just Microsoft's implementation of the C# language (sort of a name branding, as they did with Visual Basic and other "Visual" languages). However, you could say my example was a "Windows Forms" application while you are looking for a "Console" application. On to the answer...

http://www.java2s.com/Code/CSharp/Development-Class/Thisprogramaveragesalistofnu

That link will give you a C# console application that averages a list of numbers input by a user, which is very similar to the request you made. Seeing as you are just making a program to learn C#, the above program should help you learn just as effectively as a program to add some numbers together which were input by the user.


In order to use the code at the above link, you will want to open Visual Studio and create a new "Visual C#" -> "Windows" -> "Console Application". Then, in your "Program.cs" file, you will notice there is a function called "Main". There is also a function called "Main" in the above link's code. Just copy everything in the "Main" function of the above link's code and paste it into your "Main" function. Then run the program like I said in my previous answer to your question. You may notice that the console outputs your answer and closes immediately after, making it hard to read the answer in such a small amount of time. To remedy that, all you need to do is type the following line at the bottom of the code you pasted:

System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

That line of code basically tells the program to sleep for a specified period of time. In the case show above, I specified that period of time to be forever. So, in order to stop your console application from running, you will have to click the close button on the top right of the console.

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Nick

Expertise

I can answer questions regarding C#, C++, SQL, Visual Basic, .Net Framework 2.0, general programming technique, and general algorithm development. My current area of focus is C#.

Experience

C++/VB programmer since 10th grade. I used to love video game development (still do, just don't do it anymore). Have real world experience in 4 companies for a total of about 4 years non-academic experience.

Organizations
Innotrac (we deal with order fulfillment)

Publications
CodeProject.com

Education/Credentials
BS in Computer Science with minor in Entertainment Technology.

Awards and Honors
"The Next Bill Gates" gag award :-). College: Summa Cum Laude.

©2012 About.com, a part of The New York Times Company. All rights reserved.