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 8871787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:04:44+00:00 2026-06-14T18:04:44+00:00

I have foo ( object ) and foo2 ( string ) in a C#

  • 0

I have foo (object) and foo2 (string) in a C# console application. The Code 2 throws exception while Code 1 works fine.

Can you please explain why it is behaving so (with MSDN reference)?

// Code 1

object foo = null;
string test = Convert.ToString(foo).Substring(0, Convert.ToString(foo).Length >= 5 ? 5 : Convert.ToString(foo).Length);

// Code 2

string foo2 = null;
string test2 = Convert.ToString(foo2).Substring(0, Convert.ToString(foo2).Length >= 5 ? 5 : Convert.ToString(foo2).Length);
  • 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-06-14T18:04:45+00:00Added an answer on June 14, 2026 at 6:04 pm

    From the documentation of Convert.ToString(string):

    Return Value
    Type: System.String
    value is returned unchanged.

    So null input will result in a null return value.

    From the documentation of Convert.ToString(object):

    Return Value
    Type: System.String
    The string representation of value, or String.Empty if value is null.

    (Where “Nothing” means “null” here.)

    So null input will result in an empty string (non-null reference) return value.

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

Sidebar

Related Questions

Say I have the following code in code.py: from foo import bar from foo2
I have following code: class Foo(object): def __init__(self): baz=self.bar(10) @staticmethod def bar(n): if n==0:
I'm trying to create a string (foo2) which will have all the string object
Imagine you have an object foo that you saved as saved.file.rda as follows: foo
I have following test.pyx cdef public class Foo[object Foo, type FooType]: cdef public char*
I have the following structure: class foo(object): class bar(object): def __init__(self, parent): self._parent=parent #this
I have a custom object Foo with a boolean property called Flagged and when
var object = {foo: 'bar'}; Does JavaScript have a way to determine that object.foo
I have the following c# classes: class A : Object { foo() {} }
I have an object Title : foo Summary : foo bar Body : this

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.