Jump to content

Help me to understand classes in C# 4.0

Gilmer Donis's Photo
Posted Oct 28 2011 02:18 PM
2843 Views

Hello, i'm kind of new in learning C#, bought a book, Programming C# 4.0, i'm just at chapter 5, and i'm wondering, the classes most be defined in a single file for each one or a single file can contain several classes,
Thanks a lot for your time and support.

Tags:
1 Subscribe


2 Replies

0
  arjonesiii's Photo
Posted Nov 01 2011 02:03 PM

Yes, a file may contain more than one class.

Here's an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
class TestClasses
{
public string getName()
{
return "TestClasses";
}
}
class TestClasses2
{
public string getName()
{
return "TestClasses2";
}
}

}

You can paste that code into a new "class file" and then use both defined classes in your project. With that said, unless you have a good reason to place more than one class in any given file, you should avoid doing so, because it can complicate maintenance.
0
  Gilmer Donis's Photo
Posted Dec 30 2011 07:12 PM

Arjones:
I want to thank you for your answer, I didn't had the chance to thank you till now,
so far I've read a little more about c#, thanks for the tip, I'll try to keep classes as small as i can.
Best regards.