Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Private constructor

Private constructor: Private constructor is a special instance constructor with "private" access modifier in a class.

It is generally used in classes that contain static members only.

Syntax:

[Private Access Modifier] ClassName()
{
 ..
}

 

Points to remember:

A class with private constructor cannot be inherited.
You cannot create an object of the class which has private constructor only.
You can create instance of class if class contain public constructor along with private constructor.
You can use only parameterized constructor with private constructor.


Example:

class Program
    {
 
        static void Main(string[] args)
        {
            studentPrivate.InstituteName = "PLGP";
            var ITStudent = new studentPrivate("Shivaji");
            ITStudent.StudentName = "Nagnath";
            var ElectronicsStudent = new studentPrivate("Shivaji");
            ElectronicsStudent.StudentName = "Girish";
           
            ITStudent.DisplayStudentDetails();
            ElectronicsStudent.DisplayStudentDetails();
 
        }
   }
 


public class studentPrivate
    {
        public static string InstituteName;
        public string StudentName { get; set; }
        public string TeacherName;
 
     
        public studentPrivate(string teacher)
        {
            TeacherName = teacher;
        }
        //private Constructor
        private studentPrivate()
        {
            InstituteName="JNEC";
        }
 
        public void DisplayStudentDetails()
        {
            Console.WriteLine("Institute Namne: {0}", InstituteName);
            Console.WriteLine("Student Name: {0}", StudentName);
            Console.WriteLine("Teacher Namne: {0}", TeacherName);
        }
    }
 


Output:


 

Access modifiers in C#

Access modifiers in C#:

Encapsulation is one of the biggest advantages of OOP, it is a process of binding the data members and member functions into a single unit. Encapsulating data and methods ensures independency, and limited access.

You can use access modifiers to set the allowed access to not only classes, but also to all members of those classes.

C# five access Specifiers:

Public :

Accessible outside the class through object reference.

Private:

 Accessible inside the class only through member functions.

Protected :

Just like private but Accessible in derived classes also through member functions.

Internal :

Visible inside the assembly. Accessible through objects.

Protected Internal :

Visible inside the assembly through objects and in derived classes outside the assembly through member functions.

Example:

namespace TestApp
{
public class student
    {
        //private
        private string StudentCategory;
        //public
        public string studentName { get; set; }
        //internal
        internal string InstituteName { get; set; }
        public static int numberOfStudents = 0;
        public student()
        {
            numberOfStudents++;
        }
 
        public student(string caetgory)
        {
            StudentCategory = caetgory;
        }
 
        publicbool isEligibleForReservation()
        {
            if (StudentCategory !="Open")
            {
                return true;
            }
            else
                return false;
        }
    }
}


namespace TestApp
{
class Program
    {
static void Main(string[] args)
        {
            var firstStudent = new student("Open");
  //public property firstStudent.studentName = "Nagnath Kendre";
  //internal property use            firstStudent.InstituteName = "JNEC";
            Console.WriteLine("Eligible for reservation :" + firstStudent.isEligibleForReservation());
            var otherStudent = new student("OBC");
            otherStudent.studentName = "Balaji Kendre";
            otherStudent.InstituteName = "PLGPL";
            Console.WriteLine("Eligible for reservation:" + otherStudent.isEligibleForReservation());
 
            var StudentsCount=student.numberOfStudents;
 
            Console.WriteLine("Number of students :" + StudentsCount);
           
        }
     }
  }
 

 

Properties

Property is a class member that looks like fields or variables if you try to access using objects, but they use accessor methods to get and set their data.

Points to remember:

1.  This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
2.  Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.
3.  A get property accessor is used to return the property value, and a set accessor is used to assign a new value.
4.  Properties that do not implement a set accessor are read only.
5.  For auto implemented properties no custom accessor required.

get accessor:

get acessor is like method,It must return a value of the property type.
When you reference the property, except as the target of an assignment, the get accessor is invoked to read the value of the property

set accessor:

 Set accessor is like method whose return type is void. It uses an implicit parameter called value, whose type is the type of the property.
When you assign a value to the property, the set accessor is invoked by using an argument that provides the new value.

Interface Properties:

You can declare properties on interface. The accessor of an interface property does not have a body. Thus, the purpose of the accessors is to indicate whether the property is read-write, read-only, or write-only.

Syntax:

[attributes] [new] type identifier {interface-accessors}

 

more info...http://codechef4u.com/post/2015/07/02/Interface-Properties


Read-only Properties:

You can also create read-only properties if you avoid setting accessor method. For example, to

make the Name property a read-only property, you use this code:

    class Product
    {
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
        }
    }


Static Properties:

You can create static properties in C# like fields, which means you don’t need an object to use them.

    public class student
    {
        private static int numberOfStudents = 0;
        public student()
        {
            numberOfStudents++;
        }
 
        public static int NumberOfStudents
        {
 
            get
            {
                return numberOfStudents;
            }
            set
            {
                numberOfStudents =value;
            }
        }
    }

 

Auto implemented properties:

In C#3.0 and above Microsoft introduced feature called auto implemented properties. 

For auto implemented properties no custom accessor required. They also enable client code to create objects. When you declare a property, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.

Example:

public class Product
    {
        string ProductUseCategory;
 
        private string name;
 
 
        public Product(string cetgory)
        {
            ProductUseCategory =cetgory;
        }
       
        //auto implemented properties
        public int ProductID { get; set; }
 
        //simple property declartion
        public string ProductName
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
     
        public double? UnitPrice { get; set; }
        
        public bool isProductSellable()
        {
            if (ProductUseCategory== "Expired")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
       
    }