Getting Started with C# for Beginners

  • Context: C# 
  • Thread starter Thread starter Sam_
  • Start date Start date
  • Tags Tags
    Beginners
Click For Summary

Discussion Overview

The discussion focuses on getting started with C# programming for beginners, covering basic concepts, syntax, and operations in the language. Participants share code examples, explanations, and ask questions related to programming practices in C#.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant introduces a simple "Hello World" program in C# and outlines a method of tutorial that includes code followed by explanations.
  • Another participant provides an example of a program that adds two integers, explaining the purpose of each line of code and the concept of classes in C#.
  • Several participants inquire about using multiple operations in C#, suggesting variations such as subtraction and addition, and expressing interest in combining operations.
  • One participant comments on the similarities between C# and Java, suggesting that C# is an attempt to improve upon Java, while noting differences in portability and performance focus.
  • Another participant critiques the syntax of converting input from string to integer, questioning the design choice of using a utility class for conversion.
  • One participant suggests using int.Parse instead of Convert.ToInt32, while another discusses the implications of static versus non-static methods in C#.
  • There is a request for clarification on the differences between Convert.ToInt32 and int.Parse, indicating a need for deeper understanding of these methods.

Areas of Agreement / Disagreement

Participants express differing opinions on syntax preferences and the design of the C# language, particularly regarding method calls and class structures. There is no consensus on the best practices for input conversion or the implications of static methods.

Contextual Notes

Some participants express confusion over the syntax and design choices in C#, particularly regarding the use of utility classes and method types. There are unresolved questions about the implications of these design choices on programming practices.

Who May Find This Useful

Beginners interested in learning C# programming, those transitioning from other programming languages, and individuals seeking clarification on C# syntax and operations.

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 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • Sticky
  • · Replies 13 ·
Replies
13
Views
8K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
3
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K