Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only using a class within another class

Tags:

c#

c#-3.0

        public class ClientData : IEquatable<ClientData>
        {
            public String CustomerName { get; set; }
            public int CustomerId { get; set; }        

            public bool Equals(ClientData other)
            {
                if (other == null) return false;
                return (CustomerName == other.CustomerName && CustomerId == other.CustomerId);
            }

            public override int GetHashCode()
            {
                int hash = 23;
                hash = hash * 31 + CustomerName.GetHashCode();
                hash = hash * 31 + CustomerId.GetHashCode();
                return hash;
            }

        }

and

public class Service
{
    ....
}

I'm looking for a way to use my ClientData but only within my service class i.e only the service class knows that the clientdata class exists and can use its methods normally

like image 279
David Avatar asked Apr 25 '26 03:04

David


2 Answers

You can make it a nested class e.g.:

public class Service
{
    private class ClientData : IEquatable<ClientData>
    {
       ...
    }
}
like image 132
digEmAll Avatar answered Apr 26 '26 16:04

digEmAll


Make CientData a private nested class of Service:

public class Service
{
    private class ClientData
    {
        // ...
    }
}
like image 43
Anton Gogolev Avatar answered Apr 26 '26 17:04

Anton Gogolev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!