0
votes

I am relatively new in C#.

I am trying to understand how the flowing bit of code is working.

public static int Method3()
{
 //some code
   If(Class1.Method1(int Variable1).Method2(Class3 Variable2))
   {
      //even more code
   }
 //some code
}

OK, now a bit of context.

This if-statement is in Method3 and Method3 is the Class Class1.

  • Method1 takes an Int value and returns NULL or an Class Class2.

  • Method2 takes a Class lets call it Class3 and it returns true or false.

So I understand for the if-statement to be valid the condition must return true or false.

Which will come from the Method2 from my understanding.

But what is Method1 doing here? What happens with the output of Method1? Does it have any influence to the condition?

I hope you guys can understand what I mean. If not please ask.

2
Maybe it helps you understanding if you rewrite the code to var intermediateResult = Class1.Method1(int Variable1); if(intermediateResult.method2(Class3 Variable2)) { ... } - SomeBody
Mind that this will cause a NullReferenceException if Method1 returns null. - Fildor
@Fildor - yes, if Method2 is a method of the class. It could be an extension method which could deal with a null. - Corak
2 "could"s are 2 too many @Corak - Fildor
@Fildor - absolute statements are always wrong. ^_^ - Corak

2 Answers

3
votes

It would be far easier to understand if you get an example with more meaninful names.

Warning: This code and the one in your question is vulnerable to NullReferenceException. If GetClient returns null, you will have an exception.

For example:

public static bool SellingExample1()
{
   int clientId = 21;
   
   // Possible NullReferenceException
   if(Shop.GetClient(clientId).OwesMoney())
   {
      // Send warning email to sales manager
   }
   // Do selling logic
}

public static bool SellingExample2()
{
   int clientId = 21;

   Client clientToSell = Shop.GetClient(clientId);
   if (clientToSell == null) return false; // Check to avoid NullReferenceException before calling methods on a null object.

   bool clientOwesMoney = clientToSell.OwesMoney();

   if(clientOwesMoney)
   {
      // Send warning email to sales manager
   }
   // Do selling logic
}


public class Shop
{
    public static Client GetClient(int clientId)
    {
        // Look the database and return the client
    }
}

public class Client
{
    public int Id { get; set; }
    public string Name { get; set; }
    
    public bool OwesMoney()
    {
        // Return true if there are unpaid bills
    }
}
2
votes

A method doesn't take a class. It takes an instance of a class.

Think of a class as a description of a thing and an instance as a specific thing of that kind, e.g. Cat might be a class where "Tom the cat" might be an instance.

The picture is a little more complex because methods can be static meaning they belong with the class or not, meaning they belong with the instance. In the following, I'll assume you are dealing with static methods because the method in your example is static.

Because you are chaining method calls, I assume Method1 returns something (an object instance) you can call Method2 on.

Now let's look at how your code might be modified given that understanding:

public static int Method3()
{
    //some code
    int Variable1 = 42;
    Class3 Variable2 = new Class3();
    if(Class1.Method1(Variable1).Method2(Variable2))
    {
        //even more code
    }
    //some code
}