Getting Started with C# for Beginners

  • C#
  • Thread starter Sam_
  • Start date
  • Tags
    Beginners
In summary: Yep, classes are just like capsules where everything in your program goes. class HelloWorld{public static void Main(){Console.WriteLine("Hello World!");}}
  • #1
Sam_
15
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
  • #2
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?
 
  • #3
how to use multiple operations,

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

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

for example: The sum of two integers will be subtracted by another integer..
 
  • #6
int n = 5 + 5 - 7; // n will be 3
 
  • #7
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.
 
  • #8
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.)
 
  • #9
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())... ^^
 
  • #14

1. What is C# and why should I learn it?

C# (pronounced "see sharp") is a high-level, object-oriented programming language developed by Microsoft. It is widely used for developing a variety of applications, from desktop software to web development and game development. Learning C# opens up many job opportunities in the tech industry and can also help you improve your problem-solving and critical thinking skills.

2. Do I need any prior programming experience to learn C#?

While prior programming experience can be helpful, it is not necessary to learn C#. The language is designed to be beginner-friendly and has a clear syntax that is easy to understand. However, having a basic understanding of programming concepts and logic can make the learning process smoother.

3. What tools do I need to get started with C#?

To start learning C#, you will need a text editor or an integrated development environment (IDE) such as Visual Studio. These tools will allow you to write, compile, and run your C# code. You will also need the .NET framework, which is a software framework that provides a large library of pre-written code for developers to use in their applications.

4. How long does it take to learn C#?

The time it takes to learn C# varies depending on your prior programming experience, the amount of time you dedicate to studying, and your learning style. With consistent practice and dedication, you can become proficient in the basics of C# within a few months. However, mastering the language and its advanced features may take longer.

5. Are there any resources available for beginners to learn C#?

Yes, there are plenty of resources available for beginners to learn C#. You can find online tutorials, courses, and books that cover the basics of the language. Additionally, there are many online communities and forums where you can ask for help and learn from experienced programmers. Microsoft also offers extensive documentation and resources for learning C# on their official website.

Similar threads

  • Programming and Computer Science
Replies
2
Views
169
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top