C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 14 July 2016

Interview Question:- What will be the output of Default Integer Property ?


Case 1:

int x;
 Console.WriteLine(item);

o/p : Error
Reason: - Not initialized

Case 2:


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

namespace commongStringQuestion
{
    class Program
    {

        class A
        {

            public int Id { get; set; }
            public string Name { get; set; }

        }

        static void Main(string[] args)
        {
          
            HashSet<A> obj = new HashSet<A>();
            A obj3 = new A();
            Console.WriteLine(  obj3.Id);
            Console.ReadLine();
           
        }
    }
}

O/p :- 0


Case 3:- Can we initialize autoimplemented property

Yes in the constructor


class Cricket
{
    public Cricket()
    {
        PlayerName = "Sachin";
    }
    public string PlayerName { get; set; }

}


As of C#6 there is a new way:
public string PlayerName { get; set; } = "Sachin"


public string PlayerName { get; } = "Ganguly";

::-
other way is to get rid of auto implemented property and use normal property and assign the private fields in set.


Case 4:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace commongStringQuestion
{
    class Program
    {
        public class A
        {
            public string Name { get; set; }
            public string Id { get; set; }
            private string x { get; set; }
            private int counter;

            public A()
            {
                counter = 7;
            }
            public int Counter
            {
                get { return counter; }
            }
        }

        static void Main(string[] args)
        {
            A obja = new A();
           // obja.Counter = 8;//wrong
            Console.WriteLine(obja.Counter);
            Console.ReadLine();
        }
    }
}









No comments:

Post a Comment