C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet422/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   418   419   420   421   422   423   424   425   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

abstract

,

virtual

, or 

protected

.

A structure object can be created using 



new

 in the same way as a class object, but it is 

not required. When 

new

 is used, the specified constructor is called. When 



new

 is not used, 

the object is still created, but it is not initialized. Thus, you will need to perform any 

initialization manually.

Here is an example that uses a structure to hold information about a book:

// Demonstrate a structure.

using System;

// Define a structure.

struct Book {

  public string Author;

  public string Title;

  public int Copyright;

  public Book(string a, string t, int c) {

    Author = a;

    Title = t;

    Copyright = c;

  }

}

// Demonstrate Book structure.



class StructDemo {

  static void Main() {

    Book book1 = new Book("Herb Schildt",

                          "C# 3.0: The Complete Reference",

                          2009); // explicit constructor

    Book book2 = new Book(); // default constructor

    Book book3; // no constructor

    Console.WriteLine(book1.Title + " by " + book1.Author +

                      ", (c) " + book1.Copyright);

    Console.WriteLine();

www.freepdf-books.com



328

 

P a r t   I :  



T h e   C #   L a n g u a g e

    if(book2.Title == null)

      Console.WriteLine("book2.Title is null.");

    // Now, give book2 some info.

    book2.Title = "Brave New World";

    book2.Author = "Aldous Huxley";

    book2.Copyright = 1932;

    Console.Write("book2 now contains: ");

    Console.WriteLine(book2.Title + " by " + book2.Author +

                      ", (c) " + book2.Copyright);

    Console.WriteLine();

// Console.WriteLine(book3.Title); // error, must initialize first

    book3.Title = "Red Storm Rising";

    Console.WriteLine(book3.Title); // now OK

  }

}

The output from this program is shown here:



C# 3.0: The Complete Reference by Herb Schildt, (c) 2009

book2.Title is null.

book2 now contains: Brave New World by Aldous Huxley, (c) 1932

Red Storm Rising

As the program shows, a structure can be initialized either by using 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   418   419   420   421   422   423   424   425   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish