My linux world » cs Partial class / region

cs Partial class / region


To deal with better visibility you can split your code in multiples partial classes.
Also, you can deal with region.

Point of start

Let’s deal with the following implementation (MyClass.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfApp.Assets
{
    class MyClass
    {
        internal int param1 { get; set; }
 
        internal int param2 { get; set; }
 
        private void increaseParam1()
        {
            this.param1++;
        }
 
        private void increaseParam2()
        {
            this.param2++;
        }
    }
}

This code is short, but if you add more code, it will be hardest to read it (for example > 10 000 lines !!).

Region

You can use region to hide/show some part of your code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfApp.Assets
{
    class MyClass
    {
        #region "param1"
        internal int param1 { get; set; }        
 
        private void increaseParam1()
        {
            this.param1++;
        }
        #endregion
 
        #region "param2"
        internal int param2 { get; set; }
 
        private void increaseParam2()
        {
            this.param2++;
        }
        #endregion
    }
}

You can split your code in partial classes

MyClass.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfApp.Assets
{
    parial class MyClass
    {
        internal int param1 { get; set; }        
        internal int param2 { get; set; }
    }
}

MyClassMethodParam1.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfApp.Assets
{
    parial class MyClass
    {
        private void increaseParam1()
        {
            this.param1++;
        }
    }
}

MyClassMethodParam2.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace WpfApp.Assets
{
    parial class MyClass
    {
        private void increaseParam2()
        {
            this.param2++;
        }
    }
}

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.