Help with C# questions?

7    27 Mar 2016 01:32 by u/Marsog

Hi, So I've recently taken up C# as my next programming language, I'm also using Visual Studio Community 2015 as my IDE.

Here are a few questions I have so far:

* What exactly is a class? I read about it and can't seem to wrap my mind around the idea. I assume that they are used for defining something that will be used repeatedly in your program with possibly different parameters or values. * How do I combine multiple C# files together into a actual application. It seems awful if i would have to make everything in one file.

Also, could anyone give me any sort of challenge or test? I've run out of things to do unfortunately, and I would like something more challenging than

Console.WriteLine("What is your name?"); etc.

Thanks!

7 comments

4

I've got nothing to add in terms of your questions. Other people have already covered all of that ground. I do have a question for you there. What's your first programming language? Most of them have the concept of classes these days so it's weird you hadn't heard of it before.

1

I've only ever done Ruby, HTML, CSS, and Python (And very, very basic python).

1

A class is a collection of related functions and data. It is used to encapsulate data into a single object. Classes are grouped together into a namespace. Any class within a namespace can reference another class within the same namespace. To reference outside a namespace you must use a #include.

Try starting out with the C# tutorials on msdn. You will become very familiar with msdn if you continue to learn C#. Its a very comprehensive reference for all standard .Net libraries.

0

If you're looking for some good online video tutorials with examples and exercises, I use Pluralsight often. It's not free, but there is a free trial that could help you pick up the basics.

https://app.pluralsight.com/a/subscribe/step1?isTrial=True

0

Most of the question has been answered in great depth already, however there is one detail that has not yet been mentioned: the fact that classes are reference-type object and not value-type objects (like structs).

The primary difference here is that when you assign a reference type object to a variable, that variable actually becomes a reference to the assigned object. Consider the following:

namespace ReferenceEx
{
     class Program
    {
        public static void Main()
        {
            RefTypeEx objectA = new RefTypeEx(8); //A has initial value of 8
            RefTypeEx objectB = new RefTypeEx(10); //B has initial value of 10.
            objectA = objectB; //Assign B to A.
            objectA.Val = 6; //change A's value to 6. Because A is now just a reference to B, B's value also changes to 6.
            Console.WriteLine(string.Format("A is {0} and B is {1}", objectA.Val, objecB.Val);  //display the behaviour.
        }
    }
    class RefTypeEx
    {
         public int Val;
         public RefTypeEx(int input)
        {
            val = input;
        }
    }
}

A and B both start as discrete instances, each with different values.

B is then assigned to A, making both of them references to the same instance. As a consequence, as no other code looks for that old initial value of A, that object is "disposed" by the .net runtime.

For a more specific explanation, B and A start off with the same blueprint, but once instanciated, look at different chunks of memory. When we assign B to A, we give the location in memory to A and it will start looking at the exact same memory location. For all intents and purposes, B and A become different ways to access the same memory location.

0

Here's a fairly straight forward but likely challenging (and hopefully fun!) beginner level problem.

  1. Create a base type "Monster" class with a few public properties - health, defense, and offense. There will also be a virtual method called Rest that lets the monster heal a bit. Create a few types of monsters that inherit from this class all that have different default stats defined in the constructor and implement Rest differently. Perhaps some might even gain some offense or defense once they test!

  2. Create an Arena class. This class will have a method: public Monster Simulate(int numberOfMonsters) which will create an arena with "numberOfMonsters" random monsters. The arena class will simulate rounds by having two monsters fight each other until one is decided the victor. So if there are 16 monsters, you'll have 8 fights simulated in the first round. 4 in the second, and so on. After each round the winners keep fighting but get one call to Rest(). It returns the winning Monster. What happens during a round with an odd number of Monsters?

  3. Create some interesting output by narrating what happens during each fight including outputting the type of Monsters fighting.

This shouldn't be too hard but would sufficiently test a basic understanding of classes, inheritance, and the reason and implementation of using multiple files.