0% found this document useful (0 votes)
12 views9 pages

C# Cat Answers

The document provides an overview of data types in C#, categorizing them into value types (integral, floating-point, boolean, character, structs, and enumerations) and reference types (strings, arrays, classes, delegates, and objects). It also discusses literals, arrays, inheritance types, and key object-oriented programming concepts such as classes, encapsulation, inheritance, polymorphism, and abstraction. Each section includes examples and explanations of how these concepts are implemented in C#.

Uploaded by

hitlerkomanam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

C# Cat Answers

The document provides an overview of data types in C#, categorizing them into value types (integral, floating-point, boolean, character, structs, and enumerations) and reference types (strings, arrays, classes, delegates, and objects). It also discusses literals, arrays, inheritance types, and key object-oriented programming concepts such as classes, encapsulation, inheritance, polymorphism, and abstraction. Each section includes examples and explanations of how these concepts are implemented in C#.

Uploaded by

hitlerkomanam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
5 In C#, data types can be categorized into two main categories: value types and reference types. Here's a brief overview of each: Value Types 1. Integral Types: + int : Represents a 32-bit signed integer. + long : Represents a 64-bit signed integer. * short : Represents a 16-bit signed integer. + byte : Represents an 8-bit unsigned integer. 2. Floating-Point Types: + Float : Represents a single-precision 32-bit floating point. ‘* double : Represents a double-precision 64-bit floating point. + decimal : A 128-bit precise decimal used for financial calculations. 3. Boolean Type: * bool : Represents a true or false value 4, Character Type: + char : Represents a single 16-bit Unicode character. 5. Structs and Enumerations: Custom value types created using struct and enum Reference Types 1. Strings: + string : Represents a sequence of characters (immutable). 2. Arrays: * Can hold multiple values of a single type. 3. Classes: + User-defined reference types that can include data members (fields) and methods. 4. Delegates: * Type-safe function pointers that can reference methods. a Objects: + The base type from which all other types derive. © scanned with OKEN Scanner 3 In C#, literals are fixed values that are represented directly in the code. Here are the main types of literals: 1. Integer Literals «Can be represented in various formats + Decimal: 123 + Hexadecimal: 0x78 * Binary: 061111011 + Octal: (not natively supported but can be represented using decimal) 2. Floating-Point Literals + Represented using decimal or exponential notation: * Decimal: 3.14 + Exponential: 1.5e2 (equivalent to 150) 3. Character Literals + Represent a single character enclosed in single quotes: * Example: ‘A’, ‘\n' (newline character) 4. String Literals * Represent a sequence of characters enclosed in double quotes + Example: "Hello, World!" * Verbatim string literals can be created using the @ symbol: + Example: @"C:\Path\To\File” (allows multiline strings and escape sequences) 5. Boolean Literals + Represent the truth values: * true or false 6. Null Literal + Represents a null reference: + Exemple: null © scanned with OKEN Scanner —_- In C#, an array is @ collection of items stored at contiguous memory locations. It allows you to store multiple values of the same type in a single variable. Here's an overview of how arrays work in C#t: 1. Declaration and Initi ization You can declare and initialize an array in a few different ways: sharp ©) copy code int{] numbers; numbers = new int(S]; 5 string{] names = new string{] { "Alice", "Bob", "charlie" }; var colors = new{] { "Red", “Green”, “Blue” 2. Accessing Elements You can access elements in an array using an index (zero-based): ceharp Copy code numbers [0] = 10 int firstNumber = numbers[0]; // 4 3. Array Length You can get the length of an array using the Length property: sharp copy code int Length = numbers.Length; // Gets the nunber of el © scanned with OKEN Scanner 4, Multidimensional Arrays C# supports multidimensional arrays (e.g., two-dimensional arrays): caharp ©) Copy code intL,] matrix = new int[3, 3]; // 3x3 array matrix[0, 0) Assi alue e firs, nt 5. Jagged Arrays A jagged array is an array of arrays, where each sub-array can have a different length: sharp 1G) Copy code intCIt] jaggedArray = new int (3301; JaggedArray[0] = new int [2]; St suh-array with 2 elesent: JaggedArray[1] = new intt3] Second dy with 3 element: 6. Iteration You can iterate through an array using a loop: sharp Copy code foreach (var name in names) £ Console.WriteLine(nane); // Output each name 7. Common Array Methods Arrays come with useful methods, including: + Array. Sort(array) : Sorts the elements in ascending order. + Array. Reverse(array) : Reverses the order of the elements. @ scanned with OKEN Scanner Inheritance in C# is a key feature of object-oriented programming that allows a class (derived or child class) to inherit fields and methods from another class (base or parent class). This promotes code reuse and establishes a hierarchical relationship among classes. Here are the main types of inheritance in C#: 1. Single Inheritance class inherits from one base class only. This is the most straightforward type of inheritance. sharp © Copy code public class Animal { public void Speak() 4 } public class Dog : Animal { public void Bark() { ayy 2. Multilevel Inheritance Acclass can inherit from a base class, and then another class can inherit from that derived class. This creates a chain of inheritance. sharp 8 Copy code public class Animal { public void Speak() { /*...*/} public class Dog : Animal { public void Bark() { /*...*/ } public class Puppy : Dog { public void Whine() { } © scanned with OKEN Scanner 3. Hierarchical Inheritance Multiple classes can inherit from a single base class. This creates a tree-like structure. sharp public class Animal { public void Speak() { /*...*/ } public class Dog : Animal ‘ public void Bark() ¢ } public class Cat : Animal ‘ public void Meom() { /*...*/ } 4. Multiple Inheritance (Through Interfaces) Copy code C# does not support multiple inheritance for classes (i.e,, a class cannot inherit from more than one base class), but it allows a class to implement multiple interfaces. This provides a way to achieve similar functionality. sharp public interface IMovable { void Nove(); public interface IAnimal { void Speak(); public class Dog : INovable, TAnimal { public void Move() { /* i public void Speak() ¢ } 5. Hybrid Inheritance Copy code This is a combination of the above types, but C# does not support hybrid inheritance in a strict sense for classes. However, you can create complex hierarchies using interfaces and classes. © scanned with OKEN Scanner 3 Object-Oriented Programming (OP) in C# is based on several key concepts that help in organizing and structuring code effectively. Here are the main OOP concepts: 1. Classes and Objects * Class: A blueprint for creating objects. It defines properties (attributes) and methods (functions) that the objects created from the class will have. sharp Copy code public class Car { public string Model { get; set; } public void start() { + + Object: An instance of a class. Objects can hold data and perform operations defined in their class. sharp Copy code Car myCar = new Car(); // Creating a 2. Encapsulation Encapsulation is the bundling of data (attributes) and methods (functions) that operate on that data into a single unit (class). It also restricts direct access to some of the object's components, which is achieved using access modifiers. + Access Modifiers: public : Accessible from anywhere private : Accessible only within the class. protected : Accessible within the class and its derived classes. internal : Accessible within the same assembly. sharp @ Copy code public class BankAccount { private decimal balance; public void Deposit(decimal amount) { /*...*/ } public decimal GetBalance() { return balance; } + © scanned with OKEN Scanner 3. Inheritance Inheritance allows a class (derived or child class) to inherit fields and methods from another class (base or parent class). This promotes code reusability sharp 8 Copy code public class Animal { public void €at() { /*...*/ } public class Dog : Animal < public void Bark() { F 4. Polymorphism Polymorphism allows methods to do different things based on the object that it is acting upon, even if they share the same name. This can be achieved through method overriding and method overloading. + Method Overriding: Redefining a base class method in a derived class using virtual and override. esharp 8 copy code public class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks" public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } + Method Overloading: Creating multiple methods with the same name but different parameters. sharp Copy code public class Calculator { public int Add(int a, int b) { return a +b; } public double Add(double a, double b) { return a + b; } © scanned with OKEN Scanner 5. Abstraction Abstraction is the concept of hiding the complex implementation details and exposing only the necessary features of an object. This can be achieved using abstract classes and interfaces. + Abstract Class: A class that cannot be instantiated and may contain abstract methods that must be implemented by derived classes. sharp ©) copy code public abstract class Shape { public abstract double Area(); public class Circle : shape { public double Radius { get; set; } public override double Area() { return Math.PI * Radius * Radius; } ‘+ Interface: A contract that defines a set of methods and properties that a class must implement. sharp Copy code public interface IDrawable { void Draw(); public class Circle : IDrawable { public void Draw() { © scanned with OKEN Scanner

You might also like