Monday, April 27, 2015

Friend function in C++ vs internal function in C#

There is no friend function in C# instead you can achieve same functionality using internal keyword

C++ Source :

#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

class access
{
// String Variable declared as internal
public:
void SetName(string Name);
friend void print(access objAcess);
private:
string name;


};

void access::SetName(string Name)
{
name = Name;
}

void print(access objAcess)
{
cout << "\nMy name is " << objAcess.name;
}

void main( )
{
access objAcess;
objAcess.SetName("Sreeyush");
print(objAcess);
 
_getch();

}


C# source :

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

namespace InternalKeywordEx
{
    class access
    {
        // String Variable declared as internal
        private string name;
        internal void print()
        {
            Console.WriteLine("\nMy name is " + name);
        }

        internal void SetName(string Name)
        {
            name = Name;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            access objAcess = new access();

            objAcess.SetName("Sreeyush");

            objAcess.print();

            Console.ReadLine();


        }
    }
}


No comments:

Post a Comment