Acces Modifiers’te korumacılık private-protected-internal-public şeklinde git gide azalan biçimdedir
Private property,field ya da method’a sadece o blok içerisinden erişilebilir (property ve fieldlar default private’tır)
Protected’a inherite edildiği classlar içerisinden erişilebilir
Internal’a (class’lar default internal’dır) bulunduğu proje (assembly) içerisinden erişilebilir
Public’e ise istenen yerden erişilebilir
Bir class private ya da protected olamaz (sadece iç içe classlarda olabilir)
Herşeyi public yapamayız hem programcı yanlış yönlendirilebilir, hem kodun okunurluğu azalabilir
List Privilage : Bir şeyin neye ihtiyacı varsa minimumda o minimumu tanımlarız
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccessModifiers
{
class Program
{
static void Main(string[] args)
{
/*
* Acces Modifiers'te korumacılık private-protected-internal-public şeklinde git gide azalan biçimdedir
* Private property,field ya da method'a sadece o blok içerisinden erişilebilir (property ve fieldlar default private'tır)
* Protected'a inherite edildiği classlar içerisinden erişilebilir
* Internal'a (class'lar default internal'dır) bulunduğu proje (assembly) içerisinden erişilebilir
* Public'e ise istenen yerden erişilebilir
* Bir class private ya da protected olamaz (sadece iç içe classlarda olabilir)
* Herşeyi public yapamayız hem programcı yanlış yönlendirilebilir, hem kodun okunurluğu azalabilir
* List Privilage : Bir şeyin neye ihtiyacı varsa minimumda o minimumu tanımlarız**/
}
}
class Customer
{
int id; //default private int id'dir
//Private değişken sadece tanımlandığı blok içerisinde geçerlidir
private int ID { get; set; }
public void Save()
{
int id;
int ID;
}
}
class Student
{
public void Save()
{
Customer customer = new Customer();
//customer.id yi çağıramayız çünkü id private bir değişkendir
//customer.ID yi çağıramayız çünkü ID private bir değişkendir
}
}
class Person
{
private int Id { get; set; } //Private property ya da field'a blok dışında hiç bir yerden erişilemez
protected string FisrtName { get; set; } //Protected property ya da field'a ise kalıtıldığı classlardan erişilebilir
}
class Worker : Person
{
public void Save()
{
//Id katılım yapsak bile private olduğu için erişemeyiz
var x = FisrtName;
}
}
internal class Course //Bir class'ın default'u internal'dır
//Internal bir Classı, bağımlı olduğu proje (assembly) içerisinde referans ihtiyacı olmadan kullanabiliriz
{
public string Name { get; set; }
private class NestedClass
{
}
}
public class Course2
{
public string Name { get; set; }
}
}
CourseManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AccessModifiers
{
class CourseManager
{
public void Add()
{
Course course = new Course();
}
}
}
Eğer başka bir projeden bir classa erişmek istiyorsak o projeyi referanslara tanıtıp kütüphane olarak eklememiz lazım
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AccessModifiers;
namespace AccessModifiersDemo
{
class Program
{
static void Main(string[] args)
{
//Başka bir projeden class kullanmak istiyorsak o projeyi referanslara tanıtıp kütüphane olarak eklememiz lazım
//Solution Explorer>MyProject>References/RightClik>Add Reference>ProjectToBeUsed
Course2 course2 = new Course2();
course2.Name = "Mehmet";
}
}
}

Yorum bırakın