C# Getting Started with C# for Beginners

  • Thread starter Thread starter Sam_
  • Start date Start date
  • Tags Tags
    Beginners
AI Thread Summary
The discussion focuses on providing beginner-friendly guidance for learning C#, emphasizing the use of Visual Studio .NET to create simple console applications. Users are encouraged to start with a "Hello World" program and gradually progress to more complex tasks, such as performing arithmetic operations with user input. Key concepts include understanding the structure of a C# program, the role of the Main method, and variable declarations. Participants also discuss the similarities and differences between C# and Java, particularly regarding syntax and performance. The conversation highlights the importance of experimenting with code and clarifying syntactical nuances in C#.
Sam_
Messages
15
Reaction score
0
This is for very beginners, trying to start up with C#.

I'll post some short notes when I'm free. I hope to make this into a useful collection of information on C# programming language. Feel free to ask any questions.

For now, you will need Visual Studio .NET, there is an express edition here.

http://msdn2.microsoft.com/en-us/express/aa975050.aspx

try a simple Hello World for the first program. My method of tutorial will involve programs followed by explanation of what the lines do.


Start a new console application in VS and type these in:

Code:
using System;
 
public class HelloWorld
{

     public static void Main( string[] args )
     {
           Console.WriteLine( "Hello World!" );
     } 
}

GO Build>Build solution.

Then go Debug>Start Without debugging.
 
Technology news on Phys.org
Code:
using System;

  public class Addition
  {
     
     public static void Main( string[] args )
     {
       int number1;  
       int number2;  
       int sum; 

       Console.Write( "first integer: " ); 
       
       number1 = Convert.ToInt32( Console.ReadLine() );

       Console.Write( "second integer: " ); 
       
       number2 = Convert.ToInt32( Console.ReadLine() );

       sum = number1 + number2; 
       Console.WriteLine( "Sum is {0}", sum ); 
    } 
 }

Think of classes as capsules where things in your program go.

public static void Main( string[] args )

This is the main method. It is the method which automatically executes when you run the program.

int number1;
int number2;
int sum;
This declares some variable. They are of type int meaning they are integers, so no decimals.


Console.Write( "first integer: " );

number1 = Convert.ToInt32( Console.ReadLine() );


Console.Writeline is used to display text on the screen. So it tells user to enter the text

in the second line number1 variable is assigned to the value on the right.
text is input by the "Console.ReadLine" and when pressed enter it is converted to an integer value by Convert.ToInt32()

the next two lines do the same for number2.

sum = number1 + number2;

This assigns the addition of two variable to the sum variable.


Console.WriteLine( "Sum is {0}", sum );


Finally the program comes up with the answer. The {0} is tells it to insert the variable in the comma-separated list with that sign, in this case "sum".


That's it, play around and use 3 variables and maybe -, * / operators.

Good Luck!

BTW - is anyone following this?
 
how to use multiple operations,

for example: subtract and addition
division and multiplication
 
how to use multiple operation

for ex. the sum of the 2 interger(number) will be subtracted by another interger(number)..
 
How to use multiple operations

for example: The sum of two integers will be subtracted by another integer..
 
int n = 5 + 5 - 7; // n will be 3
 
Interesting. That language looks much closer to java than C++, and for some reason I thought it was like a simplified C++ for web development.
 
OrbitalPower said:
Interesting. That language looks much closer to java than C++, and for some reason I thought it was like a simplified C++ for web development.

It is more like Java than any other language: C# is Microsoft's attempt to "fix" Java. There are important differences, though. In a nutshell, Java is more portable than C# while C# has more of a focus on performance (thus its 'unsafe' code, i.e. pointers).

I've never used it for web development, but it can be used for that. (I prefer Apache and related products to ASP.)
 
number1 = Convert.ToInt32( Console.ReadLine() );
I really don't like that syntax,
If Console.ReadLine() returns a string then shouldn't ToInt32 be a member of the stirng class ? so you can write Console.ReadLine().ToInt32()

Instead Convert is some sort of general utility class, in which case shouldn't ToInt32() be a static member since you are never really creating a Convert object.
Is this a Java legacy?
 
  • #10
I would do int.Parse(Console.ReadLine()), personally.

I'm not sure what your second point is. ToInt32 is a static method, that's why you write Convert.ToInt32(blah) rather than blah.ToInt32().
 
  • #11
Thanks - I don't know c# I was just looking at it from a c++ point of view.
So built in types are also classes - that makes sense for casts to be members of int rather than standalone functions.

My 2nd point was that there is nothing syntactic to show that the Convert is static, other than knowing that Convert is a keyword/library call you can't tell it isn't a local object.
 
  • #12
mgb_phys said:
My 2nd point was that there is nothing syntactic to show that the Convert is static, other than knowing that Convert is a keyword/library call you can't tell it isn't a local object.

Or more generally, there's no way to tell a static method from a nonstatic method unless you know what is a class vs. what is an object.

Foo.Bar() could be a static method of the class Foo, or a nonstatic method of the object Foo.

The convention is to start classes with a capital letter and objects with lowercase, but this isn't a part of the language.
 
  • #13
What is the differences between this two lines?
Convert.ToInt32( Console.ReadLine() ); and int.Parse(Console.ReadLine())... ^^
 

Similar threads

Replies
6
Views
2K
Replies
3
Views
3K
Replies
3
Views
2K
Replies
13
Views
7K
Replies
3
Views
5K
Replies
2
Views
3K
Replies
18
Views
2K
Back
Top