Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 550983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:20:45+00:00 2026-05-13T11:20:45+00:00

What is the difference between using the two following statements? It appears to me

  • 0

What is the difference between using the two following statements? It appears to me that the first “as string” is a type cast, while the second ToString is an actual call to a method that converts the input to a string? Just looking for some insight if any.

Page.Theme = Session["SessionTheme"] as string;
Page.Theme = Session["SessionTheme"].ToString();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:20:45+00:00Added an answer on May 13, 2026 at 11:20 am

    If Session["SessionTheme"] is not a string, as string will return null.

    .ToString() will try to convert any other type to string by calling the object’s ToString() method. For most built-in types this will return the object converted to a string, but for custom types without a specific .ToString() method, it will return the name of the type of the object.

    object o1 = "somestring";
    object o2 = 1;
    object o3 = new object();
    object o4 = null;
    
    string s = o1 as string;  // returns "somestring"
    string s = o1.ToString(); // returns "somestring"
    string s = o2 as string;  // returns null
    string s = o2.ToString(); // returns "1"
    string s = o3 as string;  // returns null
    string s = o3.ToString(); // returns "System.Object"
    string s = o4 as string;  // returns null
    string s = o4.ToString(); // throws NullReferenceException
    

    Another important thing to keep in mind is that if the object is null, calling .ToString() will throw an exception, but as string will simply return null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the difference between the following two snippets of code: using (Object o
Is there any difference between these two statements, given the following language?? Ben likes
I am using the following code snippet to find difference between two dates and
I don't see any difference between the two functions. Using the following JavaScript code:
The following string of mine tried to find difference between two strings. But it's
Is there any difference between moving the mouse in windows using the following two
What is the difference between these two: font-style:italic font-style:oblique I tried using the W3Schools
What is the main difference between these following two ways to give a method
can somebody please tell me the difference between the following two code snippets: //Code
What's the difference between the following two pieces of code? Version B seems harder

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.