Click here to Skip to main content
15,917,702 members
Home / Discussions / C#
   

C#

 
GeneralRe: Arrays in a class definition Pin
IceWater4225-Mar-06 4:30
IceWater4225-Mar-06 4:30 
GeneralRe: Arrays in a class definition Pin
kasik25-Mar-06 6:37
kasik25-Mar-06 6:37 
GeneralRe: Arrays in a class definition Pin
IceWater426-Apr-06 19:18
IceWater426-Apr-06 19:18 
GeneralRe: Arrays in a class definition Pin
kasik7-Apr-06 5:51
kasik7-Apr-06 5:51 
QuestionDial a number using C# Pin
mrkeivan24-Mar-06 23:12
mrkeivan24-Mar-06 23:12 
QuestionDirectSound & Speakers Pin
Superwill24-Mar-06 23:00
Superwill24-Mar-06 23:00 
Questionhow to use constructors?? Pin
abdelhameed8124-Mar-06 22:56
abdelhameed8124-Mar-06 22:56 
AnswerRe: how to use constructors?? Pin
Colin Angus Mackay25-Mar-06 0:06
Colin Angus Mackay25-Mar-06 0:06 
abdelhameed81 wrote:
how to use constructors


See the program class to see how to use constructors
public class A 
{
    private string myText;

    public A(string someText)
    {
        this.myText = someText;
    }
 
    public void WriteMyText()
    {
        Console.WriteLine(someText);
    }
}

public class Program
{
    public static void Main()
    {
        A a = new A("Hello World");
        a.WriteMyText();
    }
}

abdelhameed81 wrote:
why do we use them regarding the idea that we can initialize our variables in any section without using constructors.


Because it is good practice to initialise the fields to something sensible. The CLR will initialise the fields to default values if you don't set them. e.g. an int will be set to 0, a string to "" and so on. But they may not be sensible for your application.

In objects that are immutable there is no opportunity to set the fields after the constructor has setup the object. The very nature of an immutable object is that it does not change. So it MUST set the class fields at construction. For example the string class is immutable, you cannot change a string object, you can only create new string objects.

Sometimes you may have a situation where a field is a lazy lookup because it isn't used often and it takes a long time to retrieve the value. In that case it would be initialised to null (if it is a reference type) and the getter property would do the lookup and cache the value in the field when it has been retrieved.



ColinMackay.net
Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

GeneralRe: how to use constructors?? Pin
Guffa25-Mar-06 0:58
Guffa25-Mar-06 0:58 
AnswerRe: how to use constructors?? Pin
Guffa25-Mar-06 0:55
Guffa25-Mar-06 0:55 
QuestionTAPI Pin
hafz24-Mar-06 21:39
hafz24-Mar-06 21:39 
Answercan you help me out Pin
mrkeivan25-Mar-06 0:34
mrkeivan25-Mar-06 0:34 
GeneralRe: can you help me out Pin
RizwanSharp25-Mar-06 3:09
RizwanSharp25-Mar-06 3:09 
GeneralRe: can you help me out Pin
cfsego7-May-06 21:05
cfsego7-May-06 21:05 
GeneralRe: can you help me out Pin
mrkeivan8-May-06 3:58
mrkeivan8-May-06 3:58 
QuestionCan We detect Sql Servers on Network using c# Pin
Jax_qqq24-Mar-06 19:37
Jax_qqq24-Mar-06 19:37 
QuestionHow to change textBox contents using ActiveX Pin
wajih2k24-Mar-06 19:04
wajih2k24-Mar-06 19:04 
QuestionDll's Pin
Navaneeth.K.N24-Mar-06 18:58
Navaneeth.K.N24-Mar-06 18:58 
AnswerRe: Dll's Pin
HakunaMatada24-Mar-06 21:56
HakunaMatada24-Mar-06 21:56 
QuestionUnique Registration Key Pin
Navaneeth.K.N24-Mar-06 18:56
Navaneeth.K.N24-Mar-06 18:56 
AnswerRe: Unique Registration Key Pin
HakunaMatada24-Mar-06 22:02
HakunaMatada24-Mar-06 22:02 
QuestionByte to bits conversion Pin
eggie524-Mar-06 18:55
eggie524-Mar-06 18:55 
AnswerRe: Byte to bits conversion Pin
Robert Rohde24-Mar-06 20:04
Robert Rohde24-Mar-06 20:04 
GeneralRe: Byte to bits conversion Pin
eggie524-Mar-06 20:08
eggie524-Mar-06 20:08 
GeneralRe: Byte to bits conversion Pin
Robert Rohde24-Mar-06 20:12
Robert Rohde24-Mar-06 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.