0% found this document useful (0 votes)
101 views30 pages

C# (Second Day)

This document provides an overview of key concepts in C# including namespaces, classes, data types, conditional statements, loops, methods, properties, object-oriented programming principles like encapsulation, abstraction, inheritance, and polymorphism. It discusses how to define namespaces, classes, and methods. It also covers value types vs reference types, passing arguments to methods, and differences between concepts like const vs readonly fields and ref vs out parameters.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views30 pages

C# (Second Day)

This document provides an overview of key concepts in C# including namespaces, classes, data types, conditional statements, loops, methods, properties, object-oriented programming principles like encapsulation, abstraction, inheritance, and polymorphism. It discusses how to define namespaces, classes, and methods. It also covers value types vs reference types, passing arguments to methods, and differences between concepts like const vs readonly fields and ref vs out parameters.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

C#

HCL Infosystems Ltd.


Hierarchy

 Namespace
 Classes, Structure, Enumerators
 Field, Methods, Indexeres, Delegates

 Examples
 System namesapace
 Console class
 Write() method
Importing namespaces

Use the using keyword


 
using System;
 
Console.WriteLine(“Hello”);
Creating Aliases

A method to define the alias or shortcut to some class or interface or


structure
Use the using keyword
 
using <aliasname>=<class with namespace>;
 
e.g.
using c=System.Console;
 
c.WriteLine(“Hello”);
Data types

 Divided into two categories


 Primitive or Value type
 Reference Types
Primitive or Value type

 That holds values


 Integrals
 byte – byte
 short – 2 byte
 int – 4 byte
 long – 8 byte
 sbyte
 ushort
 uint
 ulong
 Floatings
 float – 4 bytes – 6 dec place
 double – 8 bytes – 14 dec place
 decimal – 16 bytes – 28 dec place
 Character
 char – 2 bytes
 Booleans
 bool – 2 bytes
Reference Types

 string
 object
 Difference between in value type and reference type
 Value types are stored in stack and reference in heap
 Value types can never have null value assigned
View contents of a exe file or MSIL code

Use ILDASM tool to view MSIL code


 
ILDASM filename.exe
Reading Data from Keyboard

 Use ReadLine() method of Console class


 To convert data from String to other value types use
Parse() method of data types
 We can also methods from Convert class
 long Convert.ToInt64()
 int Convert.ToInt32()
 short Convert.ToInt16()
Usage of @

Neutralize the meaning of escape sequence characters


Use keyword as identifier
Remove the escape sequence effect
Break stings in multi-line
string s=@ “c:\amit”;
Spead a string into multiple lines
string s=@”Amit
Kumar
Sharma”;
Allow to use a keyword as indentfier
int @float;
Conditional Statements

 if
 switch
 ternary operator (?:)
Shortcut of if-else
Condition?true part:false part;
Loops

 while loop
 do-while loop
 for loop
 foreach loop – works with arrays and collections
 
Class and Object

 A class is a template or set of specifications to create similar set of


objects
 An object is the real entity that takes some memory space for its
fields and executes its members
Types of members

 Non-static or instance members – always require an object to call


them
 Static members or Class members – Do not require any object to
call them. Accessible directly though the class name. static keyword
is required.
Classification of members

 Fields – to hold the value


 Variable - default
 Constants – const keyword required and value must be
assigned while declaration
 const double pi=31.4;
 ReadOnly Fields – Value can be assigned directly or using
constructor. readonly keyword required
 Methods – to do some action
 Constructor
 Destructor
 Concrete Methods or General methods
 Abstract methods
 Sealed method
Constructor

 Special method which has the same name as


class
 No return type
 Used to initialize fields of a class
 Can be overloaded
 Can be private
Destructor

 Special methods which has the same name as


class name preceded by tild (~)
 Calls automatically when object releases the
memory
 Cannot be overloaded
 By default is public, public keyword is not
allowed to use
Abstract methods

 A methods having only the signature but no body


contents
 Contents are provide by overriding the methods in child
class
 If declared in class, abstract keyword is required and if
declared inside an interface, no abstract keyword
required
 If a class contains any abstract method, it must be
declared as abstract
Sealed method

 A method that can never be overridden


 sealed keyword is required
Difference between const and readonly field

 In const value must be give while declaring the


constants
 const int n=5;
 Readonly field can have the value while
declaration or using constructors
 readonly int n;
 readonly int n=5;
Properties

 Special methods that looks like fields for outside world


 set and get are used to create the properties
 
datatype variable;
<returntype> <propertyname>
{
set
{
Variable=value;
}
get
{
Return variable;
}
}
 Objectname.propetyname=anyvalue; //set
 Variable=objectname.propertyname; //get
Types of properties

 Read/Write properties
 Having set and get
 Read Only properties
 Having only get
 Write Only properties
 Having only set
Passing arguments to the methods

 Pass by value (default)


 Pass by reference (ref keyword)
 When values from actual arguments get passed to formal
arguments and if we make any changes to format arguments,
they are reflected in actual arguments
 Pass By output (out keyword)
 To send a variable to receive the output
 
Difference between ref and out

 In pass by ref, variable must be initialized by in out no initialization


required
Pillars of OOPs

 Encapsulation

 All members should be bundled in one body


Abstraction or Data Hiding

 Provides accessibility control on the members


 It is based on certain keywords
 private
 public
 protected
 internal (default)
 protected internal
Polymorphism

 A method to achieve multiple activities from an item


 It is achieve using the concept of overloading
 Method Overloading
 Operator Overloading
Inheritance

 Provides re-usability of code


 .NET allows only single inheritance
 It allows to use multiple interfaces
 Use : operator to inherit a class or interface into other
class or interface

You might also like