Web Design that taps into the haromny and vision of your dreams.

C# Aggregation and Advanced Scope Techniques

Advanced access modifiers tutorial

Written By on in C#

514 words, estimated reading time 3 minutes.

This section deals with advanced access modifiers, such as internal, aggregation and namespaces and touches on modules and assemblies.

Advanced C# Programming Series
  1. C# Advanced Data Types
  2. Using C# Properties and Indexers
  3. C# Event Handling and Delegates
  4. Method Overloading and Overriding in C#
  5. C# Class Abstraction and Encapsulation
  6. C# Aggregation and Advanced Scope Techniques
  7. Polymorphism in C#
  8. Boxing and Unboxing Value Types in C#
  9. C# Operator Overloading
  10. Creating Multi-Threading Applications with C#
  11. Unsafe Code Execution in Microsoft .Net

Previously we have seen the basic access modifiers in C#, let's start off by quickly looking at the final access modifier, the internal keyword. This is access modifier makes the code accessible to the component or assembly level. Internal classes, methods and properties are similar to the friendship concept in C++ and Visual Basic.

Internal access allows the code to be accessed from any other code compiled within the same assembly.

// Test Assembly: testAssembly.cs
// compile with /target:library
 
using System;
using System.Text;
 
namespace testAssembly
{
    public class testClass
    {
        internal static int myInt;
 
        public testClass()
        {
            myInt = 9;
        }
 
        public int GetMyInt
        {
            get { return myInt; }
        }
    }
}

Test Application:

// Test Program
// Compile with /reference:testAssembly.dll
 
using System;
using System.Text;
using testAssembly;
 
namespace testAssembly
{
    class Program
    {
        static void Main()
        {
            testClass myTest = new testClass();
 
            // Will not compile as this program is not a member
            // of the test assembly.
            myTest.myInt = 10;
 
            // OK to use the getter method.
            int demo = myTest.GetMyInt;
        }
    }
 
}

Internal methods cannot be accessed from outside the assembly they are compiled to, but you can use a getter and setter to modify the data. Internal access prevents a private field from being overwritten to allow direct access to the data.

Aggregation

Small objects are not really too useful on their own, but when lots of small objects come together and form a much larger object, then you start to see the power of object orientated programming. Aggregation is the name given to this grouping of objects.

Think of a Windows form. The form by itself isn't really much use to anybody, nor is a button, input box or drop down list, but when these objects are combined together they form an object that is useful.

The power of object orientation is in the relationships between the objects, not on the individual objects themselves. Objects are built from other objects to form object collaborations or aggregates.

This is where the internal access modifier is most used. Public objects have unlimited access, and you may wish to secure this data, but private access does not allow the objects to interact. Protected cannot be used as the objects are not inheriting from other objects.

The internal modifier allows a private-like access level to all objects within the collaboration.

You can also have a protected internal access modifier, which, as you may think, allows access to the class from the current assembly or from types derived from the containing class.

Aggregation is not the same a inheritance.

Aggregation and Inheritance both provide ways to create larger classes from smaller classes, but they do this in completely different ways. Aggregation is a relationship between objects, whereas inheritance is a relationship between classes.

There is no special coding syntax for aggregation; it is just the way that objects are constructed. In this example, the combination of the wheel, engine and brakes objects form the aggregation of the car object.

class Wheel
{
  int Diameter;
}
 
class Engine
{
  int Cylinders;
}
 
class Brakes
{
  int force;
}
 
class Car
{
  theWheels[] = new Wheel[4];
  theEngine = new Engine();
  theBrakes = new Brakes();
}

Last updated on: Friday 23rd June 2017

 

Comments
george

george

I already know what are the access modifiers (protected, internal, public, etc..) and aggregations for but I like your article it is very simple but useful
I am giving .NET trainings to our IT developers community, for sure I'll be using some parts from your article..

Reply to george

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me