Very Basic questions

elbarky

New member
Joined
Jun 10, 2015
Messages
2
Programming Experience
Beginner
Untitled.png

hey guys , im a self learning beginner so could u please explain the following. ?

  1. that high lighted line
  2. what does return fname; do does it make the string null ?
  3. why dont we make one class and add all our code in it why many classes what is the benifit of it ?
  4. why does the class have a three arrangments that i highlighted in the pic ?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,060
Location
Sydney, Australia
Programming Experience
10+
1. One of the basic tenets of object-oriented programming is inheritance. That means that you can define a class with a particular set of functionality and you can then define a second class that inherits the first class and, as a result, inherits all the functionality of the first class for free. An obvious example is the Control class in Windows Forms. The Control class defines all the common functionality that every control needs, including a Text property and a Click event. Classes that represent specific controls, e.g. Button, TextBox and Form, then inherit the Control class and thus include all that common control functionality automatically, without having to rewrite it multiple times. Each individual control class only has to include functionality specific to that control and not the common stuff. Your first highlighted line is saying that, while classes support inheritance, structures do not.

2. That `return` statement specifies what value is returned by the method. When you call a method, it does some work and then returns a single value or object. It's that `return` statement inside that determines what that value or object is.

That particular function is actually part of a property. The idea of a property is that it looks from the outside just like a simple field, i.e. member variable, but from the inside it looks like a pair of methods. That provides a number of advantages, most of which are not used in that code. The one that is is the fact that, while a property supports a `get` method for getting the value of the property and a `set` method for setting it's value, you can make the property read-only by omitting the setter, as is being done there, or you can make it write-only by omitting the getter. Neither of those things can be done with a simple field.

3. In very simple projects, you might put all your code into a single class for simplicity. It doesn't take long for projects to get big enough that that causes headaches though. By using multiple classes, you break code up by functionality and make it much easier to find the functionality you need to do a specific job. Consider the .NET Framework itself. It consists of thousands of classes containing millions of lines of code. Can you imagine what using the Framework would be like if all that code was in a single class? For one thing, you'd have to load everything, even if you were only using a small portion of the available functionality. Breaking things up makes it easier to find and use exactly what you need.

4. Firstly, there is no class. As the passage specifically points out, that code is a structure and a structure is different to a class. As for the question, I assume that you mean the second of the three highlighted lines. In that case, they are the constructor parameters. A constructor is a special method that is named after the type itself and is used to create a new instance of the type. It gets invoked in code with the `new` keyword. In the case of a structure, a constructor MUST have parameters. That means that you must pass corresponding values to the constructor when you invoke it. In that case, to create a Name object, you must pass the individual `first`, `middle` and `last` parts of the name.
 
Top Bottom