Top 50 C# Interview Questions Flashcards

1
Q

Kas yra C#?

A

C# yra moderni objektinio programavimo kalba, kurią sukūrė Microsoft. Ji veikia ant .NET frameworko ir yra labai panaši į JAVA ir C šeimos kalbas. Ši kalba naudojama kurti programinės įrangos komponentus.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Koks yra skirtumas tarp C# ir C programavimų kalbų?

A

1.

C kalba rašomas procedūrinis kodas.
C# rašomas kodas paremtas objektiniu programavimu.

2.

C kalba palaiko pointerius (nuorodas į kitas kitą duomenų adresus).
Tuo tarpu C# kalboje, pointeriai naudojami tik nesaugioje aplinkoje.

3.

C kalboje nėra autimatinio šiukšlių surinkimo.

C# kalboje yra automatinis šiukšlių surinkimas ir jį vykdo Common Language Runtime (CLR).

4.

C kalba gali būti paleidžiama cross-platform.

C# kalbos paleidimui reikalingas .NET Frameworkas.

5.

Abstrakcija - tai yra objektinio programavimo kalbos principas, kuomet yra paslepiamos implementavimo detalės parodant tik svarbiausias objekto savybes.

Naudojant C kalbą, pasiekiama žemo lygio abstrakcija.

C# kalboje galime pasiekti aukšto lygio abstrakciją.

6.

C kalba remiasi daugiau funkcijomis.

c# kodas labiau orientuotas į dizainą.

7.

C kalba daugiau naudojama komercijoje ir inžinerijoje.

C# kalba daugiau naudojama programinės įrangos kūrime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Kas yra Common Language Runtime (CLR)?

A

CLR arba Bendra kalbos vartojimo trukmė yra virtualioji mašina, taikanti .NET sistemos veikimo aplinką. Kiekviena .NET sistemos versija susideda is CLR. Jis gali vykdyti bet kokią .NET sistemoje parašytą programą, nepriklausomai nuo programavimo kalbos.
.NET sistemoje parašytas kodas verčiamas į tarpinį kodą - Microsoft tarpine kalba (MIL). Toliau “just-in-compiler” (JIT) perverčia į kompiuterio mašinos kodą. Kompiuteris šią kalbą suprasdamas gali atlikti parašytas užduotis.

CLR privalumai:
- integracija tarp kalbų;
-išimčių tvarkymą;
-sustiprintas saugumas;
-diegimo palaikymas;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Kas yra indeksai ir indeksavimas C# .NET?

A

Indeksas yra specialus savybės tipas, kuris leidžia klasei ar struktūrai būt prieinama kaip masyvui

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Koks yra JIT kompiliatoriaus procesas ?

A

Just-In-Time compiler (JIT) yra Common Language Runtime (CLR) dalis .NET frameworke, kuris yra atsakingas už .NET programų vykdymą nesvarbu, kokia .NET kalba tai būtų parašyta. .NET kodas pirma perverčiamas į Microsoft suprantamą kodą (MIT), o tuomet į mašinoms suprantamą kodą. Paskutinį virsmą atlieka Just-In-Time (JIT) kompileris. Šis kodas yra specifiškas mašinos aplinkai, kurioje ir veikia JIT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Kas yra šiukšlių surinkimas C#?

A

Šiukšlių surinkimas įgalina automatinį atminties valdymą. Kuomet yra sukuriamas objektas, tam tikra vieta atmintyje yra rezervuojama būtent jam ir su jo kūrimu susijusiem veiksmam. Tačiau po to, kai yra baigiamas objekto kūrimas ši atmintis nebeatsilaisvina ir negali būti pernaudota. Tokiu atveju šiukšlių surinkimas automatiškai atlaisvina atmintį, kurios po tokių veiksmų daugiau nebereikia.

Šiukšlių surinkimas aktyvuojamas kai patenkinama bent viena sąlyga:
- jei sistema turi mažai fizinės atminties, tokiu atveju šiukšlių surinkimas yra būtinas.
- jei objektų kūrimui numatytas atminties kiekis yra viršijamas, įsijungia šiukšlių surinkimas.
- jei iškviečiamas GC.Collect metodas, tačiau šis metodas kviečiamas tik esant neįprastomis sąlygomis, nes dažniausiai šiukšlių surinkimas įsijungiam automatiškai.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Kokie būna klasių tipai C#?

A

Abstract klasė
Partial klasė
Sealed klasė
Static klasė

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Kokie yra abstrakčios klasės ir interfaso skirtumai?

A

Abstract Class (a) Interface (i)

1.
(a) klasėje nurodomas ir deklaravimas ir išpildymas.

(i) Interface nurodomas tik deklaravimas.
_______________________________
2.
(a) Negalima paveldėti keletos klasių.

(i) Galima implementuoti keletą interfasų.
______________________________
3.
(a) Klasėje yra konstruktorius

(i) Interfase nėra konstruktoriaus.
____________________________
4.
(a) Klasėje gali būti statinių narių

(i) Interfase nėra statinių narių
_____________________________
5.
(a) Klasėje gali būti skirtingų tipų prieigos modifikatorių kaip public, private, protected ir kt.

(i) Interface yra tik public prieigos modifikatoriai, nes viskas interface yra public.
_____________________________
6.
(a) Abstrakcios klasės veikimas yra greitas.

(i) Interfasu veikimas yra lėtas, nes atitinkamų metodų ieškojimas tam tikroje klasėje reikalauja laiko.
____________________________
7.
(a) Klasė skirta implemetuoti pagrindinį klasės identitetą.

(i) Interface skirtas tam tikrom klasės galimybėms aprašyti.
_________________________________
8.
(a) Galima naudoti tik vieną abstrakčią klasę.

(i) Klasė galim implementuoti keletą interfasų.
________________________________
9.
(a) Jei dauguma implementacijų yra tos pačios rūšies ir veikimo, tuomet verta naudoti abstrakčią klasę.

(i) Jei dauguma implementacijų naudoja tik tuos pačius metodus, tada verta naudoti interface.
_______________________________
10.
(a) Abstrakti klasė gali savyje turėti metodus, laukus, constantas ir kt.

(i) Interface savyje gali turėti tik metodus.
________________________________
11.
(a) Klasė gali būti pilnai, dalinai ar visai neimplementuota.

(i) Interface turi būti pilnai implementuotas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Kas yra extension metodas C#?

A

Extension metodo konceptas leidžia pridėti naujus metodus esamoje klasėje bet nekoreguojant jau esamo kodo. Taip pat šiam metodui nereikia jokių specialių leidimų iš pagrindinės klasės, o taip pat nereikia jos re-compilint.

Pirmas perduodamasa parametras turi būtit “this Class class”. Class ir class yra klasės pavadinimas prie kurios norim pridėti šį metodą.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Kas yra paveldėjimas? Ar C# palaiko daugkartinį paveldėjimą?

A

Paveldėjimas yra svarbi objektinio programavimo dalis. Tai yra mechanizmas, kuomet viena klasė gali paveldėti savybes (fields ir metodus) iš kitos klasės.

Super klasė: klasė, kurios savybės yra paveldimos. Dar vadinama bazine klase, tėvine klase.

Sub klasė: klasė, kuri paveldi kitos klasės savybes, dar vadinama vaikine klase, išvestine klase. Vaikinė klasė gali prisidėti savo papildomų savybių ir metodų šalia paveldėtų.

Pernaudojimas: paveldėjimas suteikia kodui galimybę jį pernaudoti t.y. kai mes norime sukurti naują klasę, bet jau turime klasę, kurioje yra šiek tiek kodo, kuris tiktų ir šioje klasėje, mes galime paveldėti šią klasę ir pernaudoti tik tuos metodus ir savybes, kurias mum reikia.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Kas yra Managed ar Unmanaged kodas?

A

Kodas, kuris yra parašytas .NET frameworke yra priimtinas kaip “managed code”. Kodas yra tiesiogiai vykdomas CLR. Managed runtime aplinka gali pasiūlyti tokių paslaugų kaip šiušlių surinkimas, tipų tikrinimas, exceptionų pagavimas ir kt. ir tam nereik jokių trečios šalies papildinių. Kodas kuris parašytas Java, C# kalbomis visada yra priimtas kaip managed code.

Kodas, kuris gali būti vykdomas operacinės sistemos yra priimtas kaip “unmanaged”. Jis yra skirtas ir nutaikytas į procesoriaus architektūrą ir priklauso nuo kompiuterio architektūros. Norint pritaikyti tą patį kodą kitaip architektūrai, jį visada reik iš naujo kompiliuoti, nes kiekviena architektūra turi specifinį unmanaged codą.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Koks skirtumas yra tarp struktūros ir klasės?

A

Klasė yra vartotojo aprašytas šablonas ar prototipas, iš kurio yra kuriami objektai. Iš esmės klasė sukompiliuoja klases ir savybes į vieną vienetą (objektą).

Struktūra yra įvairių duomenų tipų kolekcija sudėta į vieną vienetą. Gana panašu į klasę, nes abu jie yra vartotojo aprašyti, abu savyje laiko daug skirtingų duomenų tipų.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Kas yra enum C#?

A

Enumeratorius (ar enum) yra reikšmių duomenų tipas. Pagrinde naudojamas priskirti pavadinimus iš eilės einantiem skaičiam, o tai padaro programą paprasta skaityti ir koreguoti. Dažniausiai enum’e aprašomos reikšmės yra iš anksto žinomos ir nesikeičiančios kaip savaitės dienos, metų laikai ir pan.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Kuo skiriasi ref ir out raktiniai žodžiai?

A

Raktinis žodis ‘ref’ yra skirtas paduoti argumentus per referencą. Tarkim jei yra padaromi pakeitimai šiom reikšmėm metode, kuriame jie yra kai argumentai, tai reikšmės atsispindės pradiniame kode.

Out raktažodis yra labai panašus kaip ‘ref’ raktažodis, bet labiau naudojamas grąžinti (return) keletai reikšmių kaip pvz tryParse metode.

Abu šie metodai neperduoda pačio kintamojo, bet tik jį atspindi.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Kas yra Boxing ir Unboxing C#?

A

C# tipų sistemoje yra pagrininiai trys duomenų tipai: reikšmių tipai (int, char, double ir kt.), reference type (objects) ir pointerių tipas. Iš esmės boxingas ir unboxingas yra konceptas kuomet verčiame reikšmių (value) tipą į pvz objektą ir atvirkščiai. Šis konceptas leidžia į tipų sistemą žiūrėti taip lyg bet kurio tipo reikšmė gali būti priimama kaip objektas.

Boxingo (implicit convertion) metu value tipą verčiant į reference tipą, value tipas visada saugomas stacke, o referenso tipas laikomas heape.
Unboxingas yra explicit convertion ir vyksta kuomet objektas verciamas į value tipą.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Kas yra savybės (properties) C#?

A

Savybės yra specialus klasės dalyvis leidžiantis paprastai skaityti, koreguoti ar skaičiuoti reikšmes. Jom yra priskiriami iš anksto nustatyti metodai (accesor) get ir set, kurie leidžia prieiti ir modifikuoti šias savybes taip kaip mum patinka, pvz read-only savybės turi tik get metodą.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Kas yra dalinė (partial) klasė C#?

A

Dalinė klasė yra ypatinga C# savybė. Ji suteikia galimybę implemetuoti vienos klasės funkcionalumą į keletą failų, bet kai aplikacija sukompiliuojama, šie failai tampa viena klase.

Jei Voldemortas būtų klasė , tai kiekvienas horokrusas būtų dalinė klasė (vienas iš daugelio failų). Po kompiliacijos visi horokrusai taptų tiesiog Voldemortu.

Dalinė klasė sukuriama naudojant “partial” raktažodį. Šis raktažodis naudingas skaidant metodų, interfacų ar strukturų funkcionalumą į keletą failų:

public partial Class_name
{
// Code
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Koks yra skirtumas tarp late binding ir early binding C# kalboje?

A

Early binding: Visual Basic kompileris atlieka procesą vadinamą surišimu (binding), kuomet objektas yra priskiriamas tam tikram kintamajam. Objektas yra early bound, kuomet jis yra priskiriamas tam tikro TIKSLAUS tipo kintamajams. Early bind objektai leidžia kompileriui rezervuoti atminties ir atlikti kitus optimizacijos veiksmus prieš paleidžiant aplikaciją.

Palyginimui - objektas yra late bound kuomet jis yra priskiriamas Object tipo kintamajam. Šio tipo objektai gali turėtų įvairių sąsajų su kitais objektais, todėl tipas bus nustatytas tik kompiliavimo metu. Late binding negali naudotis visual studio siūlomom pagalbom, pvz kaip auto-completion, nes tai vyksta tik žinant tikslų kintamojo tipą.

Early binding procesas yra žymiai greitesnis nei late binding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Kokius žinote būdus overloadint metodus C#?

A

Metodų overloadinimas yra gana dažnas būdas implemetuoti polimorfizmą. Tai yra galimybė aprašyti tą patį metodą kita forma. Vartotojas gali aprašyti funkciją kurios pavadinimas bus toks pats, tačiau skirsis paduodami parametrai, jų tvarka, parametrų tipai ir kt. tačiau negali pasikartoti metodo kurio parametrai, jų išdėstymas ir tipai bus tokie pat kaip kito metodo toje pačioje klasėje.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is Reflection in C#?

A

Reflection is the process of describing the metadata of types, methods, and fields in a code. The namespace System. Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods, and value types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Kuo skiriasi konstantos nuo read-only laukai C#?

A

C# kalboje konstanta dažniausiai pastovūs laukai ir vietinės reikšmės kintamieji. Kontantos reikšmė nekinta visoje programoje t.y. kai reikšmė yra nustatyta ji neturi būti pakeista. Konstanta yra skaičius, string, boolean ir kt. Readonly raktažodis parodo, kad priskirimas kintamajam galimas tik tuo atveju kai deklaruojam kitamajį arba klasės konstruktoriuje, kuriame jis yra apibrėžtas.

22
Q

Ar galima parašyti programą, kurioje keletas catch teiginių būtų vykdoma iš eilės?

A

Pagrindinis catch bloko panaudojimas yra sugauti iškylančius exceptionus, todėl jei bandome pagauti ir NullException ir kokį ArgumentException’ą galima ir parašyti keletą catch teiginių einančių iš eilės. Tačiau jei bus parašyti keletas catch blokų tam pačiam exceptionui bus išmestas compile-time erroras.

Tuo atveju jei programa meta keletą errorų skirtingose vietose, o try-catch bloke yra jie visi iš eilės surašyti, bus pagautas tik pirmas išmestas erroras ir toliau pereita prie vykdomosios dalies.

23
Q

What is Jagged Arrays?

A

A jagged array is an array of arrays such that member arrays can be of different sizes. In other words, the length of each array index can differ. The elements of Jagged Array are reference types and initialized to null by default. Jagged Array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns.

24
Q

What is the System. String and System.Text.StringBuilder classes?

A

C# StringBuilder is similar to Java StringBuilder. A String object is immutable, i.e. a String cannot be changed once created. Every time when you use any of the methods of the System. String class, then you create a new string object in memory. For example, a string “GeeksForGeeks” occupies memory in the heap, now, changing the initial string “GeeksForGeeks” to “GFG” will create a new string object on the memory heap instead of modifying the initial string at the same memory location. In situations where you need to perform repeated modifications to a string, we need the StringBuilder class. To avoid string replacing, appending, removing, or inserting new strings in the initial string C# introduce StringBuilder concept. StringBuilder is a dynamic object. It doesn’t create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.

25
Q

What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ?

A

The System.Array.CopyTo() technique makes a replica of the components into another existing array. It makes copies the components of one cluster to another existing array. The Clone() technique returns a new array object containing every one of the components in the first array. The Clone() makes a duplicate of an array as an object, consequently should be cast to the real exhibit type before it tends to be utilized to do definitely. The clone is of a similar type as the first Array.

26
Q

What is the difference between to dispose and finalize methods in C#?

A

The primary difference between dispose() and finalize() is that the dispose() must be explicitly invoked by the user and the finalize() is called by the garbage collector when the object is destroyed.

27
Q

Kas yra delegatai C#?

A

Delegatas yra objektas, kuris turi nuorodą į kitą metodą. Iš esmės delegatus galima įsivaizduoti kaip užprogramuotą mygtuką, kurį paspaudus įvyks kokia tik nori funkcija. Svarbiausia kad iškviečiamas metodas grąžintų tokį patį duomenų tipą, kaio nurodyta delegato apraše.

28
Q

Kas yra sealed klasė C#?

A

Sealed klasės yra naudojamos norint apriboti vartotojų galimybę paveldėti klases. Klasė tampa sealed panaudojus “sealed” raktažodį.

sealed class class_name
{
// data members
// methods
}
Metodai taip pat gali būti sealed ir tokiais tapę jie negali būti perrašomi(override). Tačiau metodai gali tapti sealed klasėse, kurios buvo paveldėtos. Norint nurodyti metodą kaip sealed, tėvinėje klasėje jis turi būti nurodytas kaip ‘virtual’.

29
Q

What is the Constructor Chaining in C#?

A

We can call an overloaded constructor from another constructor using this keyword but the constructor must belong to the same class because this keyword is pointing to the members of the same class in which this is used. This type of calling the overloaded constructor is also termed as Constructor Chaining.

30
Q

What is a multicasting delegate in C#?

A

Multicasting of delegate is an extension of the normal delegate(sometimes termed as Single Cast Delegate). It helps the user to point more than one method in a single call.

31
Q

Kas yra Generics C#?

A

Generic yra klasė, kuri leidžia vartotojui aprašyti klases ir metodus su tam tikru placeholedriu. Pagrindinė mintis naudojant Generics yra leisti tipams (Integer, String…ir kt., taip pat ir vartotojo aprašytus tipus) būti metodų, klasių ir interfacų parametrais.
Pagrindinė kolekcijų limitacija yra efektyvaus tipų tikrinimo nebuvimas. Tai reiškia, kad galima įdėti betkokį objektą į kolekciją, nes visos klasės C# kalboj yra paveldėtos iš object bazinės klasės. Tai kompromituoja tipų saugumą ir prieštarauja bazinę apibrėžtį, kad C# yra type-safe kalba.

32
Q

Describe Accessibility Modifiers in C#?

A

Prieigos modifikatoriai yra raktažodžiai, kurie apibūdina prieinanumo lygį klasei, jos nariui, duomenų tipui, kuri yra aprašyta programoje. Pagrinde jie yra naudojami apriboti prieigą siekiant apsaugoti duomenis nuo išorės naudotojų. Yra 4 prieigos modifikatoriai (public, protected, internal, private), kurie nusako iš viso 6 prieinamumo lygius:

public
private
private protected
protected
internal
protected internal

33
Q

Kas yra Virtual metodas C#?

A

C# virtualų metodą mes galime implemetuoti tiek tėvinėje klasėje tiek vaikinėje klasėje. Jis naudojamas kai metodo esminis veikimas yra labai panašus, tačiau prireikia papildomo funkcionalumo. Šis metodas deklaruojamas tėvinėje klasėje, o perrašyti ar aprašyti naują jo veikimą galima vaikinėje klasėje. Tėvinėje klasėje prie pavadinimo turi prisirašyti ‘virtual’, vaikinėje klasėje naudojamas ‘override’. Šis metodas dar žinomas kaip polimorfizmas.

34
Q

What is Multithreading with .NET?

A

Multi-threading is a process that contains multiple threads within a single process. Here each thread performs different activities. For example, we have a class and this call contains two different methods, now using multithreading each method is executed by a separate thread. So the major advantage of multithreading is it works simultaneously, which means multiple tasks execute at the same time. And also maximizing the utilization of the CPU because multithreading works on time-sharing concept mean each thread takes its own time for execution and does not affect the execution of another thread, this time interval is given by the operating system.

35
Q

Kas yra Hash lentelių klasė C# ?

A

Hashtable klasė atstovauja key-value porų rinkinį, kurie surikiuoti pagal hash kodą (raktą). Ši klasė atkeliauja su System.Collections namespace’u. Hashtable suteikia įvairius metodų tipus, kurie gali atlikti įvairias operacijas šiai lentelei. Raktai lentelėje yra naudojami pasiekti esamus kolekcijos elementus. Labai didelei hashtable lentelei bendras dydis gali būti padidintas iki 2 milijardų elementų 64-bitų sistemai.

36
Q

Kas yra LINQ C#?

A

LINQ (Language Integrated Query) pirmą kartą pristatyta .NET 3.5 ir Visual Studio 2008. Didžiausias privalumos jos yra tai kad kalba suteikia .NET kalboms (C#, VB.NET ir kt.) sugeneruoti querius gauti duomenims iš duomenų bazės.

37
Q

Kodėl private virtual metodas negali būti perrašomas?

A

Norint perrašyti virtual metodą, vaikinėje klasėje reik sukurti jo override’ą. Kadangi tėvinėje klasėje šis metodas yra private, dėl to vaikinėje klasėje jis bus nepasiekiamas.

38
Q

What is File Handling in C#?

A

Generally, the file is used to store the data. The term File Handling refers to the various operations like creating the file, reading from the file, writing to the file, appending the file, etc. There are two basic operations that are mostly used in file handling is reading and writing of the file. The file becomes stream when we open the file for writing and reading. A stream is a sequence of bytes that is used for communication. Two streams can be formed from the file one is the input stream which is used to read the file and another is the output stream is used to write in the file. In C#, the System.IO namespace contains classes that handle input and output streams and provide information about file and directory structure.

39
Q

List down the commonly used types of exceptions?

A

An exception is an error that happens at runtime. Using C#’s exception-handling subsystem, we can, during a structured and controlled manner, handle runtime errors. The primary advantage of exception handling is that it automates much of the error handling code. An Exception handling is additionally important because C# defines standard exceptions for common program errors, like divide-by-zero or index-out-of-range.

C# Exception with their meaning:

ArrayTypeMismatchException: This exception comes when the Type of value being stored is incompatible with the type of the array.
DivideByZeroException: It comes when the user tries to division an integer value by zero.
IndexOutOfRangeException: When an array index is out-of-bounds, it exception occurred.
InvalidCastException: A runtime cast is invalid.
OutOfMemoryException: Insufficient free memory exists to continue program execution.
OverflowException: An arithmetic overflow occurred.
NullReferenceException: An attempt was made to operate on a null reference—that is, a reference that does not refer to an object.

40
Q

What is the difference between “is” and “as” operators in C#?

A

C# includes three keywords that support runtime type identification: is, as, and typeof.

is operator: We can determine if an object is of a particular type by using the is operator. Its general form is shown here:

expr is type

Here, expr is an expression that describes an object whose type is being tested against type. If the type of expr is that the same as, or compatible with, type, then the result of this operation is true. Otherwise, it is false. Thus, if the result is true, expr is a form of type. Because it applies to is, one type is compatible with another if both are the equivalent of type, or if a reference, boxing, or unboxing conversion exists.

As operator: Sometimes if we want to try a conversion at runtime, but not throw an exception if the conversion fails (which is the case when a cast is used). To do this, use the as operator, which has this general form:

expr as type

Here, expr is the expression being converted to type. If the conversion succeeds, then a reference to type is returned. Else, a null reference is returned. The as the operator can be used to perform only reference, boxing, unboxing, or identity conversions. The as operator offers a streamlined alternative to is in some cases.

41
Q

What is Singleton design pattern in C#?

A

Singleton design pattern in C# is a common design pattern. In this pattern, a class has just one instance in the program that gives global access to it. Or we can say that a singleton is a class that permits only one instance of itself to be made and usually gives simple access to that instance.

There are different approaches to carry out a singleton design in C#. Coming up next are the regular attributes of a singleton design.

Private and parameterizes single constructor
Sealed class.
Static variable to hold a reference to the single made example
A public and static method of getting the reference to the made example.

42
Q

How to implement a singleton design pattern in C#?

A

We can implement a singleton design pattern in C# using:

No Thread Safe Singleton.
Thread-Safety Singleton.
Thread-Safety Singleton using Double-Check Locking.
Thread-safe without a lock.
Using .NET 4’s Lazy type.

43
Q

What is tuple in C#?

A

The word Tuple means “a data structure which consists of the multiple parts”. So tuple is a data structure that gives you the easiest way to represent a data set that has multiple values that may/may not be related to each other. It was introduced in .NET Framework 4.0. In tuple, you can add elements from 1 to 8. If you try to add elements greater than eight, then the compiler will throw an error. Tuples are generally used when you want to create a data structure that contains objects with their properties and you don’t want to create a separate type for that.

44
Q

What are Events?

A

An event is a notification that some action has occurred. Delegates and events are related because an event is built upon a delegate. Both expand the set of programming tasks to which C# can be applied. It is an important C# feature is built upon the foundation of delegates: the event. An event is, essentially, an automatic notification that some action has occurred. Events work like this:

An object that has an interest in an event registers an event handler for that event. When the event occurs, all registered handlers are called. Event handlers are represented by delegates.

Events are members of a class and are declared using the event keyword. Its most commonly used form is shown here:

event event-delegate event-name;

Here, event-delegate is the name of the delegate used to support the event, and event-name is the name of the specific event object being declared.

45
Q

What is the difference between Dispose() and Finalize() methods?

A

The main difference between both methods is that Dispose() method is used to release the unmanaged resources of an object while Finalize is also used for the same purpose but it doesn’t guarantee the garbage collection of an object. Another major difference is that dispose() method is explicitly invoked by the user and finalize() method is invoked by the garbage collector, just before the object is destroyed.

46
Q

What is the difference between Array and ArrayList?

A

An array is a group of like-typed variables that are referred to by a common name. ArrayList represents an ordered collection of an object that can be indexed individually. It is basically an alternative to an array. Below are the major differences:

Feature,
Array,
ArrayList
_______________
Memory,
This has fixed size and can’t increase or decrease dynamically,
Size can be increased or decreased dinamically
_______________
NameSpace,
Arrays belongs to System.Array namespace,
ArrayList belongs to System.Collection namespace
_______________
DataType,
In Arrays, we can store only one datatype either int, string, char, etc.,
In ArrayLists we can store different datatype variables
_______________
Operation speed,
Insertion and deletion operation is fast,
Insertion and deletion operation in ArrayList is slower that an Array
_______________
Typed,
Arrays are strongly typed which means it can store only specific type of items or elements,
ArrayListss are not strongly typed
_______________
null,
Array cannot accept null,
ArrayList can accept null

47
Q

Write a Features of Generics in C#?

A

Generics is a technique that improves your programs in many ways such as:

It helps you in code reuse, performance, and type safety.
You can create your own generic classes, methods, interfaces, and delegates.
You can create generic collection classes. The .NET Framework class library contains many new generic collection classes in System.Collections.Generic namespace.
You can get information on the types used in generic data types at run-time.

48
Q

Difference between SortedList and SortedDictionary in C#.

A

SortedList a collection of key/value pairs that are sorted according to keys. By default, this collection sort the key/value pairs in ascending order. It is of both generic and non-generic types of collection.

SortedDictionary a generic collection that is used to store the key/value pairs in the sorted form and the sorting is done on the key.

Below are some differences between SortedList and SortedDictionary:

SL: The memory of Sorted List is an overhead;
SD: The memory of Sorted Dictionary is not bottlenecked;
______________
SL: In SortedList the elements are stored in a continuous block in memory;
SD: In SortedDictionary the elements are stored in separate object that can spread all over the heap;
______________
SL: In SortedList the memory fragmentation is high;
SD: In SortedDictionary the memory fragmentation is low;
______________
SL: It require less memory for storage;
SD: it require more memory for storage;
______________
SL: In SortedList less inserts aand delete operations are required;
SD: In SortedDictionary more inserts and delete operations are required;
______________
SL: In SortedList you can access elements using the index;
SD: In SortedDictionary you can access elements using index or key. Here key access is sufficient, there is not need of accessing elements using index.
______________
SL: In SortedList data are already in sorted form;
SD: In SortedDictionary data are in un-sorted form.
______________

49
Q

What are namespaces in C#?

A

It provides a way to keep one set of names(like class names) different from other sets of names. The biggest advantage of using namespace is that the class names which are declared in one namespace will not clash with the same class names declared in another namespace. It is also referred as named group of classes having common features.

50
Q

Who can be the members of namespaces in C#?

A

The members of a namespace can be namespaces, interfaces, structures, and delegates.

51
Q

Kokios bent 4 kolekcijos gali būti naudojamos objektų laikmenai iš Generic klasės?

A
  1. Dictionary (+ Sorted Dictionary)
  2. List ( + Sorted List)
  3. HashSet
  4. LinkedList
  5. Set