Search This Blog

Showing posts with label .Net Interview Questions. Show all posts
Showing posts with label .Net Interview Questions. Show all posts

Friday, March 4, 2016

Difference between ? and ?? operators in C#

Difference between ? and ??
            The conditional operator (?:) returns one of two values depending on the value of a       Boolean expression. Following is the syntax for the conditional operator.
      condition ? first_expression : second_expression;    
              // ?: conditional operator.
            int input = Convert.ToInt32(Console.ReadLine());           
            string classify = (input > 0) ? "positive" : "negative";

       The ?? operator is called the null-coalescing operator. It returns the left-hand operand if          the operand is not null; otherwise it returns the right hand operand.

            int? x = null;
            // Set y to the value of x if x is NOT null; otherwise,
            // if x = null, set y to -1.
            int y = x ?? -1;

Difference between as and is in C#


as
is
Is Operator is used to check the Compatibility of an Object with a given Type and it returns the result as a Boolean (True or false).
As Operator is used for Casting of Object to a given Type or a Class.
Ex:
if (someObject is StringBuilder) ...
Ex:
object x = 5;
// int y = x as int; // not allowed becoz of int : value type
int? y = x as int?; // allowed becoz of nullable type

Ex.         
Student s = obj as Student;                   
is equivalent to:
Student s = obj is Student ? (Student)obj : (Student)null;


as operator should be used with Reference Type or nullable type

Friday, January 29, 2016

Difference between Readonly and Const in C# with example

Difference between Readonly and Const in C# with example
Constant

Constant fields are defined at the time of declaration in the code snippet, because once they are defined they can't be modified. By default a constant is static, so you can't define them static from your side.

It is also mandatory to assign a value to them at the time of declaration otherwise it will give an error during compilation of the program snippet. That's why it is also called a compile-time constant.
Readonly

A Readonly field can be initialized either at the time of declaration or within the constructor of the same class. We can also change the value of a Readonly at runtime or assign a value to it at runtime (but in a non-static constructor only).

For that reason a Readonly field is also called a run-time constant.

public class ReadOnlyConstDynamicVar
    {
        public const int x = 10; // assigning value is mandatory
        public readonly int y ; // assigning value is optional

        public ReadOnlyConstDynamicVar()
        {
            //readonly takes default value if nothing is assigned
            y = 20;
        }

        public string GetreadOnlyValue()
        {           
            //y = 20;  // not allowed to change readonly variable
            const int g = 30;
            return g.ToString();
        }
    }

       static void Main(string[] args)
        {           
            ReadOnlyConstDynamicVar obj = new ReadOnlyConstDynamicVar();
            //reading const value
            Console.WriteLine(ReadOnlyConstDynamicVar.x.ToString());
            //reading readonly value
            Console.WriteLine(obj.y.ToString());

            Console.ReadLine();
        }


Friday, November 27, 2015

How to read web.config appsettings or connection strings from .aspx file

How to read web.config appsettings or connection strings from .aspx file

Web.Config:

<appSettings><
add key="appSettingsKey" value="Testing Input"/></appSettings>
<connectionStrings><
add name="con_string" connectionString="Data Source=TestServer;Initial Catalog=TestDB;Integrated Security=SSPI" providerName="System.Data.SqlClient"/></connectionStrings>

.aspx Page

<asp:TextBox ID="txtAppSetting" runat="server" Text="<%$appSettings:appSettingsKey %>"></asp:TextBox>
<asp:TextBox ID="txtConnectionString" runat="server" Text="<%$connectionStrings:con_string %>"></asp:TextBox>

by using <%$appSettings:appSettingsKey %> , we can read the app settings data of Web.Config

Thursday, January 15, 2015

calling ModalPopupExtender from server side (code behind) in C# & using RequiredFieldValidator in ModalPopupExtender of AjaxControlToolkit

How to use RequiredFieldValidator in ModalPopupExtender (Ajax control tool kit):

by default validators are not work in ModalPopupExtender without using
ValidationGroup check the below example for more information

How to use ModalPopupExtender from code behind (server side code) in C# :


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<
asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager><asp:UpdatePanel ID="UpdatePanelNavigation" UpdateMode="Always" runat="server"><ContentTemplate>
<asp:Button ID="btnUpdate" runat="server" Text="Edit Details" Width="150px" OnClick="btnUpdate_Click" Visible="true" />
<asp:HiddenField ID="hdnForModelDetails" runat="server" />
<cc1:ModalPopupExtender ID="mpeDetails" runat="server"TargetControlID="hdnForModelDetails" PopupControlID="pnlUpdateDetails"BackgroundCssClass="modalBackground" DropShadow="false"CancelControlID="btnIFSCCancel" />
<asp:Panel ID="pnlUpdateDetails" DefaultButton="btnIFSCOk" runat="server" CssClass="modalPopup"><table><tr><td style="padding-left: 15px; text-align: left; vertical-align: top">IFSC Code:</td><td><asp:TextBox ID="txtIfscCode" runat="server" MaxLength="34"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtIfscCode" runat="server" ErrorMessage="Please enter IFSC code" Display="Dynamic" ValidationGroup="IFSCValidationGroup"></asp:RequiredFieldValidator></td></tr></table><br />
<div style="text-align: center"><asp:Button ID="btnIFSCOk" Text="OK" Width="80px" runat="server" OnClick="btnIFSCOk_Click" ValidationGroup="IFSCValidationGroup" /><asp:Button ID="btnIFSCCancel" Text="Cancel" Width="80px" runat="server" OnClick="btnIFSCCancel_Click" /></div>
</asp:Panel></ContentTemplate></asp:UpdatePanel>
Code:

using System;public partial class Default3 : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
}

protected void btnUpdate_Click(object sender, EventArgs e){
mpeDetails.Show();
}

protected void btnIFSCCancel_Click(object sender, EventArgs e){
}

protected void btnIFSCOk_Click(object sender, EventArgs e){
}
}

Popular Posts