locked
Class file RRS feed

  • Question

  • Hi,
    I am working on c# class file and normally i define one public class in one class file. E.g myClassA in class1.cs  and  myClassB in class2.cs.    Is it possible to combine myClassA and myClassB inside class1.cs  as i'm not sure whether one class file can have different classes or not.

    Thanks
    Thursday, September 18, 2008 5:00 AM

Answers

  •  In C# you can combine two or more class in a namespace (you can save a namespace in .cs file) 

    e.g:

    //File A.cs

    namespace A
    {
        class B
        {
            
        }

        class C
        {
            
        }

        class D
        {
            
        }
    }
    you can further read about namespace at 
    http://msdn.microsoft.com/en-us/library/z2kcy19k(VS.80).aspx


    You can also spit a single class using partial class in C#. For more reading on partial class you can go here
    http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx

    Hope it will help you.

    Regards,


    Haider Raza Software Engineer, ZABSolutions. MCP(C#)
    • Edited by Haider Raza Naqvi Thursday, September 18, 2008 5:14 AM
    • Proposed as answer by Harry Zhu Friday, September 19, 2008 2:49 AM
    • Marked as answer by jack 321 Monday, September 22, 2008 7:21 AM
    Thursday, September 18, 2008 5:11 AM
  • You can define any number of classes you want in a .cs file (regardless if you specify a namespace or not).

    Mattias, C# MVP
    • Marked as answer by jack 321 Monday, September 22, 2008 7:21 AM
    Thursday, September 18, 2008 8:11 AM

All replies

  •  In C# you can combine two or more class in a namespace (you can save a namespace in .cs file) 

    e.g:

    //File A.cs

    namespace A
    {
        class B
        {
            
        }

        class C
        {
            
        }

        class D
        {
            
        }
    }
    you can further read about namespace at 
    http://msdn.microsoft.com/en-us/library/z2kcy19k(VS.80).aspx


    You can also spit a single class using partial class in C#. For more reading on partial class you can go here
    http://msdn.microsoft.com/en-us/library/wa80x488(VS.80).aspx

    Hope it will help you.

    Regards,


    Haider Raza Software Engineer, ZABSolutions. MCP(C#)
    • Edited by Haider Raza Naqvi Thursday, September 18, 2008 5:14 AM
    • Proposed as answer by Harry Zhu Friday, September 19, 2008 2:49 AM
    • Marked as answer by jack 321 Monday, September 22, 2008 7:21 AM
    Thursday, September 18, 2008 5:11 AM
  • You can define any number of classes you want in a .cs file (regardless if you specify a namespace or not).

    Mattias, C# MVP
    • Marked as answer by jack 321 Monday, September 22, 2008 7:21 AM
    Thursday, September 18, 2008 8:11 AM